3 """Utilities for dealing with the webcams."""
11 from decorator_utils import memoized
12 import smart_home.device as dev
14 logger = logging.getLogger(__name__)
17 class BaseChromecast(dev.Device):
22 def __init__(self, name: str, mac: str, keywords: str = "") -> None:
23 super().__init__(name.strip(), mac.strip(), keywords)
25 now = datetime.datetime.now()
27 BaseChromecast.refresh_ts is None
28 or (now - BaseChromecast.refresh_ts).total_seconds() > 60
30 logger.debug('Refreshing the shared chromecast info list')
31 if BaseChromecast.browser is not None:
32 BaseChromecast.browser.stop_discovery()
33 BaseChromecast.ccasts, BaseChromecast.browser = pychromecast.get_chromecasts(
36 atexit.register(BaseChromecast.browser.stop_discovery)
37 BaseChromecast.refresh_ts = now
40 for cc in BaseChromecast.ccasts:
41 if cc.cast_info.host == ip:
42 logger.debug(f'Found chromecast at {ip}: {cc}')
44 self.cast.wait(timeout=1.0)
46 raise Exception(f'Can\'t find ccast device at {ip}, is that really a ccast device?')
49 return self.cast.is_idle
56 def get_friendly_name(self):
63 def get_model_name(self):
64 return self.cast.model_name
67 def get_cast_type(self):
68 return self.cast.cast_type
72 return self.cast.app_id
74 def get_app_display_name(self):
75 return self.cast.app_display_name
77 def get_media_controller(self):
78 return self.cast.media_controller
83 app = self.get_app_display_name()
84 mc = self.get_media_controller()
86 return f'{app} / {status.title}'
88 def start_app(self, app_id, force_launch=False):
89 """Start an app on the Chromecast."""
90 self.cast.start_app(app_id, force_launch)
93 """Tells the Chromecast to quit current app_id."""
96 def volume_up(self, delta=0.1):
97 """Increment volume by 0.1 (or delta) unless it is already maxed.
98 Returns the new volume.
100 return self.cast.volume_up(delta)
102 def volume_down(self, delta=0.1):
103 """Decrement the volume by 0.1 (or delta) unless it is already 0.
104 Returns the new volume.
106 return self.cast.volume_down(delta)
110 f"Chromecast({self.cast.socket_client.host!r}, port={self.cast.socket_client.port!r}, "
111 f"device={self.cast.cast_info.friendly_name!r})"