From: Scott Date: Tue, 18 Jan 2022 21:13:21 +0000 (-0800) Subject: Fix chromecasts to work with new version of pychromecast; rename X-Git-Url: https://wannabe.guru.org/gitweb/?a=commitdiff_plain;ds=sidebyside;h=87f478b20c8b24eed6c1e87de47ba01e3d9746ab;p=python_utils.git Fix chromecasts to work with new version of pychromecast; rename some cameras, add debugging log messages. --- diff --git a/smart_home/cameras.py b/smart_home/cameras.py index 51a95e9..2cf2de4 100644 --- a/smart_home/cameras.py +++ b/smart_home/cameras.py @@ -13,7 +13,8 @@ class BaseCamera(dev.Device): camera_mapping = { 'cabin_drivewaycam': 'cabin_driveway', 'outside_backyard_camera': 'backyard', - 'outside_driveway_camera': 'driveway', + 'outside_driveway_camera_wired': 'driveway', + 'outside_driveway_camera_wifi': 'driveway', 'outside_doorbell_camera': 'doorbell', 'outside_front_door_camera': 'front_door', 'crawlspace_camera': 'crawlspace', diff --git a/smart_home/chromecasts.py b/smart_home/chromecasts.py index 08290e5..5e11ac0 100644 --- a/smart_home/chromecasts.py +++ b/smart_home/chromecasts.py @@ -2,6 +2,8 @@ """Utilities for dealing with the webcams.""" +import atexit +import datetime import logging import time @@ -14,12 +16,32 @@ logger = logging.getLogger(__name__) class BaseChromecast(dev.Device): + ccasts = [] + refresh_ts = None + browser = None + def __init__(self, name: str, mac: str, keywords: str = "") -> None: super().__init__(name.strip(), mac.strip(), keywords) ip = self.get_ip() - self.cast = pychromecast.Chromecast(ip) - self.cast.wait() - time.sleep(0.1) + now = datetime.datetime.now() + if ( + BaseChromecast.refresh_ts is None + or (now - BaseChromecast.refresh_ts).total_seconds() > 60 + ): + logger.debug('Refreshing the shared chromecast info list') + BaseChromecast.ccasts, BaseChromecast.browser = pychromecast.get_chromecasts() + atexit.register(BaseChromecast.browser.stop_discovery) + BaseChromecast.refresh_ts = now + + self.cast = None + for cc in BaseChromecast.ccasts: + if cc.cast_info.host == ip: + logger.debug(f'Found chromecast at {ip}: {cc}') + self.cast = cc + self.cast.wait() + time.sleep(0.1) + if self.cast is None: + raise Exception(f'Can\'t find ccast device at {ip}, is that really a ccast device?') def is_idle(self): return self.cast.is_idle @@ -84,5 +106,6 @@ class BaseChromecast(dev.Device): def __repr__(self): return ( f"Chromecast({self.cast.socket_client.host!r}, port={self.cast.socket_client.port!r}, " - f"device={self.cast.device!r})" + f"device={self.cast.cast_info.friendly_name!r})" ) + diff --git a/smart_home/registry.py b/smart_home/registry.py index 75fe052..7349081 100644 --- a/smart_home/registry.py +++ b/smart_home/registry.py @@ -185,6 +185,9 @@ class SmartHomeRegistry(object): return device.Device(name, mac, kws) except Exception as e: logger.exception(e) + logger.debug( + f'Device {name} at {mac} with {kws} confused me, returning a generic Device' + ) return device.Device(name, mac, kws) msg = f'{mac} is not a known smart home device, returning None' logger.warning(msg)