summaryrefslogtreecommitdiff
path: root/smart_home/chromecasts.py
diff options
context:
space:
mode:
authorScott Gasch <[email protected]>2021-10-31 13:08:51 -0700
committerScott Gasch <[email protected]>2021-10-31 13:08:51 -0700
commiteb9e6df32ed696158bf34dba6464277b648f5c74 (patch)
tree54ae278562008b0ffa834b2d34b2158d9119dc65 /smart_home/chromecasts.py
parent05a26ae305adfc47f38b8534ec8f35640df3955e (diff)
Ugh, a bunch of things. @overrides. --lmodule. Chromecasts. etc...
Diffstat (limited to 'smart_home/chromecasts.py')
-rw-r--r--smart_home/chromecasts.py88
1 files changed, 88 insertions, 0 deletions
diff --git a/smart_home/chromecasts.py b/smart_home/chromecasts.py
new file mode 100644
index 0000000..08290e5
--- /dev/null
+++ b/smart_home/chromecasts.py
@@ -0,0 +1,88 @@
+#!/usr/bin/env python3
+
+"""Utilities for dealing with the webcams."""
+
+import logging
+import time
+
+import pychromecast
+
+from decorator_utils import memoized
+import smart_home.device as dev
+
+logger = logging.getLogger(__name__)
+
+
+class BaseChromecast(dev.Device):
+ 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)
+
+ def is_idle(self):
+ return self.cast.is_idle
+
+ @memoized
+ def get_uuid(self):
+ return self.cast.uuid
+
+ @memoized
+ def get_friendly_name(self):
+ return self.cast.name
+
+ def get_uri(self):
+ return self.cast.url
+
+ @memoized
+ def get_model_name(self):
+ return self.cast.model_name
+
+ @memoized
+ def get_cast_type(self):
+ return self.cast.cast_type
+
+ @memoized
+ def app_id(self):
+ return self.cast.app_id
+
+ def get_app_display_name(self):
+ return self.cast.app_display_name
+
+ def get_media_controller(self):
+ return self.cast.media_controller
+
+ def status(self):
+ if self.is_idle():
+ return 'idle'
+ app = self.get_app_display_name()
+ mc = self.get_media_controller()
+ status = mc.status
+ return f'{app} / {status.title}'
+
+ def start_app(self, app_id, force_launch=False):
+ """Start an app on the Chromecast."""
+ self.cast.start_app(app_id, force_launch)
+
+ def quit_app(self):
+ """Tells the Chromecast to quit current app_id."""
+ self.cast.quit_app()
+
+ def volume_up(self, delta=0.1):
+ """Increment volume by 0.1 (or delta) unless it is already maxed.
+ Returns the new volume.
+ """
+ return self.cast.volume_up(delta)
+
+ def volume_down(self, delta=0.1):
+ """Decrement the volume by 0.1 (or delta) unless it is already 0.
+ Returns the new volume.
+ """
+ return self.cast.volume_down(delta)
+
+ 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})"
+ )