3 """Utilities for dealing with the webcams."""
12 from decorator_utils import memoized
13 import smart_home.device as dev
15 logger = logging.getLogger(__name__)
18 class BaseChromecast(dev.Device):
23 def __init__(self, name: str, mac: str, keywords: str = "") -> None:
24 super().__init__(name.strip(), mac.strip(), keywords)
26 now = datetime.datetime.now()
28 BaseChromecast.refresh_ts is None
29 or (now - BaseChromecast.refresh_ts).total_seconds() > 60
31 logger.debug('Refreshing the shared chromecast info list')
32 BaseChromecast.ccasts, BaseChromecast.browser = pychromecast.get_chromecasts()
33 atexit.register(BaseChromecast.browser.stop_discovery)
34 BaseChromecast.refresh_ts = now
37 for cc in BaseChromecast.ccasts:
38 if cc.cast_info.host == ip:
39 logger.debug(f'Found chromecast at {ip}: {cc}')
44 raise Exception(f'Can\'t find ccast device at {ip}, is that really a ccast device?')
47 return self.cast.is_idle
54 def get_friendly_name(self):
61 def get_model_name(self):
62 return self.cast.model_name
65 def get_cast_type(self):
66 return self.cast.cast_type
70 return self.cast.app_id
72 def get_app_display_name(self):
73 return self.cast.app_display_name
75 def get_media_controller(self):
76 return self.cast.media_controller
81 app = self.get_app_display_name()
82 mc = self.get_media_controller()
84 return f'{app} / {status.title}'
86 def start_app(self, app_id, force_launch=False):
87 """Start an app on the Chromecast."""
88 self.cast.start_app(app_id, force_launch)
91 """Tells the Chromecast to quit current app_id."""
94 def volume_up(self, delta=0.1):
95 """Increment volume by 0.1 (or delta) unless it is already maxed.
96 Returns the new volume.
98 return self.cast.volume_up(delta)
100 def volume_down(self, delta=0.1):
101 """Decrement the volume by 0.1 (or delta) unless it is already 0.
102 Returns the new volume.
104 return self.cast.volume_down(delta)
108 f"Chromecast({self.cast.socket_client.host!r}, port={self.cast.socket_client.port!r}, "
109 f"device={self.cast.cast_info.friendly_name!r})"