summaryrefslogtreecommitdiff
path: root/smart_home/chromecasts.py
blob: 08290e538262a1d2a56646f769d3e82e69798334 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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})"
        )