forked from fondberg/spotcast
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sensor.py
57 lines (46 loc) · 1.68 KB
/
sensor.py
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
import logging
import json
from homeassistant.helpers.entity import Entity
from homeassistant.util import dt
from homeassistant.const import (STATE_OK, STATE_UNKNOWN)
_LOGGER = logging.getLogger(__name__)
def setup_platform(hass, config, add_devices, discovery_info=None):
add_devices([ChromecastDevicesSensor()])
class ChromecastDevicesSensor(Entity):
def __init__(self):
self._state = STATE_UNKNOWN
self._chromecast_devices = []
self._attributes = {
'devices_json': [],
'last_update': None
}
self.update()
@property
def name(self):
return 'Chromecast Devices'
@property
def state(self):
return self._state
@property
def device_state_attributes(self):
"""Return the state attributes."""
return self._attributes
def update(self):
import pychromecast
_LOGGER.info('updating')
self._chromecast_devices = pychromecast.get_chromecasts()
_LOGGER.info('Found chromecast devices: %s', self._chromecast_devices)
# self._attributes['devices'] = [cast.name for cast in self._chromecast_devices]
chromecasts = []
for cast in self._chromecast_devices:
device = {
'name': cast.name,
'cast_type': cast.cast_type,
'model_name': cast.model_name,
'uuid': str(cast.uuid),
'manufacturer': cast.device.manufacturer
}
chromecasts.append(device)
self._attributes['devices_json'] = json.dumps(chromecasts, ensure_ascii=False)
self._attributes['last_update'] = dt.now().isoformat('T')
self._state = STATE_OK