Support CO2/PM2.5/Light sensors in HomeKit (#13804)

* Support co2/light/air sensor in HomeKit
* Add tests
* Added tests
* changed device_class lux to light
This commit is contained in:
Yonsm authored and cdce8p committed 2018-04-12 15:01:41 +02:00
1 parent f47572d3c0
commit c863b9614c
7 files changed
+318 -47

No files matched your search

@@ -41,6 +41,13 @@ class TestGetAccessories(unittest.TestCase):
"""Test if mock type was called."""
self.assertTrue(self.mock_type.called)
def test_sensor_temperature(self):
"""Test temperature sensor with device class temperature."""
with patch.dict(TYPES, {'TemperatureSensor': self.mock_type}):
state = State('sensor.temperature', '23',
{ATTR_DEVICE_CLASS: 'temperature'})
get_accessory(None, state, 2, {})
def test_sensor_temperature_celsius(self):
"""Test temperature sensor with Celsius as unit."""
with patch.dict(TYPES, {'TemperatureSensor': self.mock_type}):
@@ -56,12 +63,66 @@ class TestGetAccessories(unittest.TestCase):
get_accessory(None, state, 2, {})
def test_sensor_humidity(self):
"""Test humidity sensor with device class humidity."""
with patch.dict(TYPES, {'HumiditySensor': self.mock_type}):
state = State('sensor.humidity', '20',
{ATTR_DEVICE_CLASS: 'humidity'})
get_accessory(None, state, 2, {})
def test_sensor_humidity_unit(self):
"""Test humidity sensor with % as unit."""
with patch.dict(TYPES, {'HumiditySensor': self.mock_type}):
state = State('sensor.humidity', '20',
{ATTR_UNIT_OF_MEASUREMENT: '%'})
get_accessory(None, state, 2, {})
def test_air_quality_sensor(self):
"""Test air quality sensor with pm25 class."""
with patch.dict(TYPES, {'AirQualitySensor': self.mock_type}):
state = State('sensor.air_quality', '40',
{ATTR_DEVICE_CLASS: 'pm25'})
get_accessory(None, state, 2, {})
def test_air_quality_sensor_entity_id(self):
"""Test air quality sensor with entity_id contains pm25."""
with patch.dict(TYPES, {'AirQualitySensor': self.mock_type}):
state = State('sensor.air_quality_pm25', '40', {})
get_accessory(None, state, 2, {})
def test_co2_sensor(self):
"""Test co2 sensor with device class co2."""
with patch.dict(TYPES, {'CarbonDioxideSensor': self.mock_type}):
state = State('sensor.airmeter', '500',
{ATTR_DEVICE_CLASS: 'co2'})
get_accessory(None, state, 2, {})
def test_co2_sensor_entity_id(self):
"""Test co2 sensor with entity_id contains co2."""
with patch.dict(TYPES, {'CarbonDioxideSensor': self.mock_type}):
state = State('sensor.airmeter_co2', '500', {})
get_accessory(None, state, 2, {})
def test_light_sensor(self):
"""Test light sensor with device class lux."""
with patch.dict(TYPES, {'LightSensor': self.mock_type}):
state = State('sensor.light', '900',
{ATTR_DEVICE_CLASS: 'light'})
get_accessory(None, state, 2, {})
def test_light_sensor_unit_lm(self):
"""Test light sensor with lm as unit."""
with patch.dict(TYPES, {'LightSensor': self.mock_type}):
state = State('sensor.light', '900',
{ATTR_UNIT_OF_MEASUREMENT: 'lm'})
get_accessory(None, state, 2, {})
def test_light_sensor_unit_lux(self):
"""Test light sensor with lux as unit."""
with patch.dict(TYPES, {'LightSensor': self.mock_type}):
state = State('sensor.light', '900',
{ATTR_UNIT_OF_MEASUREMENT: 'lux'})
get_accessory(None, state, 2, {})
def test_binary_sensor(self):
"""Test binary sensor with opening class."""
with patch.dict(TYPES, {'BinarySensor': self.mock_type}):
+87 -4
View File
@@ -3,7 +3,8 @@ import unittest
from homeassistant.components.homekit.const import PROP_CELSIUS
from homeassistant.components.homekit.type_sensors import (
TemperatureSensor, HumiditySensor, BinarySensor, BINARY_SENSOR_SERVICE_MAP)
TemperatureSensor, HumiditySensor, AirQualitySensor, CarbonDioxideSensor,
LightSensor, BinarySensor, BINARY_SENSOR_SERVICE_MAP)
from homeassistant.const import (
ATTR_UNIT_OF_MEASUREMENT, ATTR_DEVICE_CLASS, STATE_UNKNOWN, STATE_ON,
STATE_OFF, STATE_HOME, STATE_NOT_HOME, TEMP_CELSIUS, TEMP_FAHRENHEIT)
@@ -40,6 +41,7 @@ class TestHomekitSensors(unittest.TestCase):
self.hass.states.set(entity_id, STATE_UNKNOWN,
{ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS})
self.hass.block_till_done()
self.assertEqual(acc.char_temp.value, 0.0)
self.hass.states.set(entity_id, '20',
{ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS})
@@ -63,14 +65,95 @@ class TestHomekitSensors(unittest.TestCase):
self.assertEqual(acc.char_humidity.value, 0)
self.hass.states.set(entity_id, STATE_UNKNOWN,
{ATTR_UNIT_OF_MEASUREMENT: "%"})
self.hass.states.set(entity_id, STATE_UNKNOWN)
self.hass.block_till_done()
self.assertEqual(acc.char_humidity.value, 0)
self.hass.states.set(entity_id, '20', {ATTR_UNIT_OF_MEASUREMENT: "%"})
self.hass.states.set(entity_id, '20')
self.hass.block_till_done()
self.assertEqual(acc.char_humidity.value, 20)
def test_air_quality(self):
"""Test if accessory is updated after state change."""
entity_id = 'sensor.air_quality'
acc = AirQualitySensor(self.hass, 'Air Quality', entity_id,
2, config=None)
acc.run()
self.assertEqual(acc.aid, 2)
self.assertEqual(acc.category, 10) # Sensor
self.assertEqual(acc.char_density.value, 0)
self.assertEqual(acc.char_quality.value, 0)
self.hass.states.set(entity_id, STATE_UNKNOWN)
self.hass.block_till_done()
self.assertEqual(acc.char_density.value, 0)
self.assertEqual(acc.char_quality.value, 0)
self.hass.states.set(entity_id, '34')
self.hass.block_till_done()
self.assertEqual(acc.char_density.value, 34)
self.assertEqual(acc.char_quality.value, 1)
self.hass.states.set(entity_id, '200')
self.hass.block_till_done()
self.assertEqual(acc.char_density.value, 200)
self.assertEqual(acc.char_quality.value, 5)
def test_co2(self):
"""Test if accessory is updated after state change."""
entity_id = 'sensor.co2'
acc = CarbonDioxideSensor(self.hass, 'CO2', entity_id, 2, config=None)
acc.run()
self.assertEqual(acc.aid, 2)
self.assertEqual(acc.category, 10) # Sensor
self.assertEqual(acc.char_co2.value, 0)
self.assertEqual(acc.char_peak.value, 0)
self.assertEqual(acc.char_detected.value, 0)
self.hass.states.set(entity_id, STATE_UNKNOWN)
self.hass.block_till_done()
self.assertEqual(acc.char_co2.value, 0)
self.assertEqual(acc.char_peak.value, 0)
self.assertEqual(acc.char_detected.value, 0)
self.hass.states.set(entity_id, '1100')
self.hass.block_till_done()
self.assertEqual(acc.char_co2.value, 1100)
self.assertEqual(acc.char_peak.value, 1100)
self.assertEqual(acc.char_detected.value, 1)
self.hass.states.set(entity_id, '800')
self.hass.block_till_done()
self.assertEqual(acc.char_co2.value, 800)
self.assertEqual(acc.char_peak.value, 1100)
self.assertEqual(acc.char_detected.value, 0)
def test_light(self):
"""Test if accessory is updated after state change."""
entity_id = 'sensor.light'
acc = LightSensor(self.hass, 'Light', entity_id, 2, config=None)
acc.run()
self.assertEqual(acc.aid, 2)
self.assertEqual(acc.category, 10) # Sensor
self.assertEqual(acc.char_light.value, 0.0001)
self.hass.states.set(entity_id, STATE_UNKNOWN)
self.hass.block_till_done()
self.assertEqual(acc.char_light.value, 0.0001)
self.hass.states.set(entity_id, '300')
self.hass.block_till_done()
self.assertEqual(acc.char_light.value, 300)
def test_binary(self):
"""Test if accessory is updated after state change."""
entity_id = 'binary_sensor.opening'
+49 -33
View File
@@ -2,13 +2,15 @@
import unittest
import voluptuous as vol
import pytest
from homeassistant.core import callback
from homeassistant.components.homekit.accessories import HomeBridge
from homeassistant.components.homekit.const import HOMEKIT_NOTIFY_ID
from homeassistant.components.homekit.util import (
show_setup_message, dismiss_setup_message, convert_to_float,
temperature_to_homekit, temperature_to_states, ATTR_CODE)
temperature_to_homekit, temperature_to_states, ATTR_CODE,
density_to_air_quality)
from homeassistant.components.homekit.util import validate_entity_config \
as vec
from homeassistant.components.persistent_notification import (
@@ -20,6 +22,52 @@ from homeassistant.const import (
from tests.common import get_test_home_assistant
def test_validate_entity_config():
"""Test validate entities."""
configs = [{'invalid_entity_id': {}}, {'demo.test': 1},
{'demo.test': 'test'}, {'demo.test': [1, 2]},
{'demo.test': None}]
for conf in configs:
with pytest.raises(vol.Invalid):
vec(conf)
assert vec({}) == {}
assert vec({'alarm_control_panel.demo': {ATTR_CODE: '1234'}}) == \
{'alarm_control_panel.demo': {ATTR_CODE: '1234'}}
def test_convert_to_float():
"""Test convert_to_float method."""
assert convert_to_float(12) == 12
assert convert_to_float(12.4) == 12.4
assert convert_to_float(STATE_UNKNOWN) is None
assert convert_to_float(None) is None
def test_temperature_to_homekit():
"""Test temperature conversion from HA to HomeKit."""
assert temperature_to_homekit(20.46, TEMP_CELSIUS) == 20.5
assert temperature_to_homekit(92.1, TEMP_FAHRENHEIT) == 33.4
def test_temperature_to_states():
"""Test temperature conversion from HomeKit to HA."""
assert temperature_to_states(20, TEMP_CELSIUS) == 20.0
assert temperature_to_states(20.2, TEMP_FAHRENHEIT) == 68.4
def test_density_to_air_quality():
"""Test map PM2.5 density to HomeKit AirQuality level."""
assert density_to_air_quality(0) == 1
assert density_to_air_quality(35) == 1
assert density_to_air_quality(35.1) == 2
assert density_to_air_quality(75) == 2
assert density_to_air_quality(115) == 3
assert density_to_air_quality(150) == 4
assert density_to_air_quality(300) == 5
class TestUtil(unittest.TestCase):
"""Test all HomeKit util methods."""
@@ -39,21 +87,6 @@ class TestUtil(unittest.TestCase):
"""Stop down everything that was started."""
self.hass.stop()
def test_validate_entity_config(self):
"""Test validate entities."""
configs = [{'invalid_entity_id': {}}, {'demo.test': 1},
{'demo.test': 'test'}, {'demo.test': [1, 2]},
{'demo.test': None}]
for conf in configs:
with self.assertRaises(vol.Invalid):
vec(conf)
self.assertEqual(vec({}), {})
self.assertEqual(
vec({'alarm_control_panel.demo': {ATTR_CODE: '1234'}}),
{'alarm_control_panel.demo': {ATTR_CODE: '1234'}})
def test_show_setup_msg(self):
"""Test show setup message as persistence notification."""
bridge = HomeBridge(self.hass)
@@ -83,20 +116,3 @@ class TestUtil(unittest.TestCase):
self.assertEqual(
data[ATTR_SERVICE_DATA].get(ATTR_NOTIFICATION_ID, None),
HOMEKIT_NOTIFY_ID)
def test_convert_to_float(self):
"""Test convert_to_float method."""
self.assertEqual(convert_to_float(12), 12)
self.assertEqual(convert_to_float(12.4), 12.4)
self.assertIsNone(convert_to_float(STATE_UNKNOWN))
self.assertIsNone(convert_to_float(None))
def test_temperature_to_homekit(self):
"""Test temperature conversion from HA to HomeKit."""
self.assertEqual(temperature_to_homekit(20.46, TEMP_CELSIUS), 20.5)
self.assertEqual(temperature_to_homekit(92.1, TEMP_FAHRENHEIT), 33.4)
def test_temperature_to_states(self):
"""Test temperature conversion from HomeKit to HA."""
self.assertEqual(temperature_to_states(20, TEMP_CELSIUS), 20.0)
self.assertEqual(temperature_to_states(20.2, TEMP_FAHRENHEIT), 68.4)