This repository has been archived by the owner on Jul 23, 2021. It is now read-only.
/
hue_sensor.py
135 lines (113 loc) · 4.29 KB
/
hue_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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"""
Sensor for checking the status of Hue sensors.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.hue_sensors/
"""
import logging
from datetime import timedelta
import requests
import json
from homeassistant.const import (CONF_FILENAME)
from homeassistant.helpers.entity import Entity
from homeassistant.util import Throttle
DOMAIN = 'hue_sensor'
_LOGGER = logging.getLogger(__name__)
SCAN_INTERVAL = timedelta(seconds=1)
PHUE_CONFIG_FILE = 'phue.conf'
REQUIREMENTS = ['hue-sensors==1.2']
def load_conf(filepath):
"""Return the URL for API requests."""
with open(filepath, 'r') as file_path:
data = json.load(file_path)
ip_add = next(data.keys().__iter__())
username = data[ip_add]['username']
url = 'http://' + ip_add + '/api/' + username
return url
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the Hue sensors."""
import hue_sensors as hs
filename = config.get(CONF_FILENAME, PHUE_CONFIG_FILE)
filepath = hass.config.path(filename)
url = load_conf(filepath) + '/sensors'
data = HueSensorData(url, hs.parse_hue_api_response)
data.update()
sensors = []
for key in data.data.keys():
sensors.append(HueSensor(key, data))
add_devices(sensors, True)
class HueSensorData(object):
"""Get the latest sensor data."""
def __init__(self, url, parse_hue_api_response):
"""Initialize the data object."""
self.url = url
self.data = None
self.parse_hue_api_response = parse_hue_api_response
# Update only once in scan interval.
@Throttle(SCAN_INTERVAL)
def update(self):
"""Get the latest data."""
response = requests.get(self.url)
if response.status_code != 200:
_LOGGER.warning("Invalid response from API")
else:
self.data = self.parse_hue_api_response(response.json())
class HueSensor(Entity):
"""Class to hold Hue Sensor basic info."""
ICON = 'mdi:run-fast'
def __init__(self, hue_id, data):
"""Initialize the sensor object."""
self._hue_id = hue_id
self._data = data # data is in .data
self._icon = None
self._name = self._data.data[self._hue_id]['name']
self._model = self._data.data[self._hue_id]['model']
self._state = self._data.data[self._hue_id]['state']
self._attributes = {}
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def state(self):
"""Return the state of the sensor."""
return self._state
@property
def icon(self):
"""Icon to use in the frontend, if any."""
return self._icon
@property
def device_state_attributes(self):
"""Attributes."""
return self._attributes
def update(self):
"""Update the sensor."""
self._data.update()
self._state = self._data.data[self._hue_id]['state']
if self._model == 'SML':
self._icon = 'mdi:run-fast'
self._attributes['light_level'] = self._data.data[
self._hue_id]['light_level']
self._attributes['battery'] = self._data.data[
self._hue_id]['battery']
self._attributes['last_updated'] = self._data.data[
self._hue_id]['last_updated']
self._attributes['lux'] = self._data.data[
self._hue_id]['lux']
self._attributes['dark'] = self._data.data[
self._hue_id]['dark']
self._attributes['daylight'] = self._data.data[
self._hue_id]['daylight']
self._attributes['temperature'] = self._data.data[
self._hue_id]['temperature']
elif self._model == 'RWL':
self._icon = 'mdi:remote'
self._attributes['last_updated'] = self._data.data[
self._hue_id]['last_updated']
self._attributes['battery'] = self._data.data[
self._hue_id]['battery']
elif self._model == 'ZGP':
self._icon = 'mdi:remote'
self._attributes['last_updated'] = self._data.data[
self._hue_id]['last_updated']
elif self._model == 'Geofence':
self._icon = 'mdi:cellphone'