Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding illumination sensor #47

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 15 additions & 19 deletions components/sensor/xiaomi.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
if device['model'] == 'sensor_ht':
devices.append(XiaomiSensor(device, 'Temperature', 'temperature', gateway))
devices.append(XiaomiSensor(device, 'Humidity', 'humidity', gateway))
elif device['model'] == 'gateway':
devices.append(XiaomiSensor(device, 'Illumination', 'illumination', gateway))
add_devices(devices)


Expand All @@ -35,24 +37,6 @@ def __init__(self, device, name, data_key, xiaomi_hub):
self._battery = None
XiaomiDevice.__init__(self, device, name, xiaomi_hub)

@property
def _is_humidity(self):
return self._data_key == 'humidity'

@property
def _is_temperature(self):
return self._data_key == 'temperature'

@property
def available(self):
"""Return True if entity is available."""
if self._is_temperature and self.current_value != 100:
return True
elif self._is_humidity and self.current_value != 0:
return True

return False

@property
def state(self):
"""Return the name of the sensor."""
Expand All @@ -65,11 +49,23 @@ def unit_of_measurement(self):
return TEMP_CELSIUS
elif self._data_key == 'humidity':
return '%'
elif self._data_key == 'illumination':
return 'lm'

def parse_data(self, data):
"""Parse data sent by gateway"""
value = data.get(self._data_key)
if value is None:
return False
self.current_value = int(value) / 100
if self._data_key == 'illumination':
self.current_value = int(value)
elif self._data_key == 'temperature' or 'humidity':
self.current_value = int(value) / 100
if self._data_key == 'temperature' and current_value == 100:
return False
if self._data_key == 'humidity' and current_value == 0:
return False
if self._data_key == 'illumination' and self.current_value == 0:
return False
self.current_value = current_value
return True
9 changes: 6 additions & 3 deletions components/xiaomi.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def _discover_devices(self):
sensors = ['sensor_ht']
binary_sensors = ['magnet', 'motion', 'switch', '86sw1', '86sw2', 'cube']
switches = ['plug', 'ctrl_neutral1', 'ctrl_neutral2']
lights = ['gateway']
gateway = ['gateway']

for sid in sids:
cmd = '{"cmd":"read","sid":"' + sid + '"}'
Expand All @@ -297,8 +297,8 @@ def _discover_devices(self):
device_type = 'binary_sensor'
elif model in switches:
device_type = 'switch'
elif model in lights:
device_type = 'light'
elif model in gateway:
device_type = 'gateway'
else:
_LOGGER.error('Unsupported devices : %s', model)
continue
Expand All @@ -308,6 +308,9 @@ def _discover_devices(self):
"sid":resp["sid"],
"short_id":resp["short_id"],
"data":data}
if device_type == 'gateway':
self.devices['light'].append(xiaomi_device)
self.devices['sensor'].append(xiaomi_device)
self.devices[device_type].append(xiaomi_device)
return True

Expand Down