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):
22 lock = threading.Lock()
24 def __init__(self, name: str, mac: str, keywords: str = "") -> None:
25 super().__init__(name.strip(), mac.strip(), keywords)
27 now = datetime.datetime.now()
28 with BaseChromecast.lock as l:
30 BaseChromecast.refresh_ts is None
31 or (now - BaseChromecast.refresh_ts).total_seconds() > 60
33 logger.debug('Refreshing the shared chromecast info list')
34 if BaseChromecast.browser is not None:
35 BaseChromecast.browser.stop_discovery()
36 BaseChromecast.ccasts, BaseChromecast.browser = pychromecast.get_chromecasts(
39 atexit.register(BaseChromecast.browser.stop_discovery)
40 BaseChromecast.refresh_ts = now
43 for cc in BaseChromecast.ccasts:
45 cc.cast_info.host == ip
46 and cc.cast_info.cast_type != 'group'
48 logger.debug(f'Found chromecast at {ip}: {cc}')
50 self.cast.wait(timeout=1.0)
52 raise Exception(f'Can\'t find ccast device at {ip}, is that really a ccast device?')
55 return self.cast.is_idle
62 def get_friendly_name(self):
69 def get_model_name(self):
70 return self.cast.model_name
73 def get_cast_type(self):
74 return self.cast.cast_type
78 return self.cast.app_id
80 def get_app_display_name(self):
81 return self.cast.app_display_name
83 def get_media_controller(self):
84 return self.cast.media_controller
89 app = self.get_app_display_name()
90 mc = self.get_media_controller()
92 return f'{app} / {status.title}'
94 def start_app(self, app_id, force_launch=False):
95 """Start an app on the Chromecast."""
96 self.cast.start_app(app_id, force_launch)
99 """Tells the Chromecast to quit current app_id."""
102 def volume_up(self, delta=0.1):
103 """Increment volume by 0.1 (or delta) unless it is already maxed.
104 Returns the new volume.
106 return self.cast.volume_up(delta)
108 def volume_down(self, delta=0.1):
109 """Decrement the volume by 0.1 (or delta) unless it is already 0.
110 Returns the new volume.
112 return self.cast.volume_down(delta)
116 f"Chromecast({self.cast.socket_client.host!r}, port={self.cast.socket_client.port!r}, "
117 f"device={self.cast.cast_info.friendly_name!r})"