Add binary sensor platform to zwave_mqtt (#35519)

* add binary sensor platform to zwave_mqtt

* add tests for binary_sensor

* fix tests

* device class is required value

* Update homeassistant/components/zwave_mqtt/binary_sensor.py

use colon as separator

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* extend tests

* code optimize

* add test for enabling a legacy binary sensor

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Marcel van der VeldtandMartin Hjelmare authored and GitHub committed 2020-05-12 20:07:11 +02:00
1 parent dd32324e01
commit 1351ddd11e
8 files changed
+589 -6

No files matched your search

+24
View File
@@ -66,3 +66,27 @@ async def sensor_msg_fixture(hass):
message = MQTTMessage(topic=sensor_json["topic"], payload=sensor_json["payload"])
message.encode()
return message
@pytest.fixture(name="binary_sensor_msg")
async def binary_sensor_msg_fixture(hass):
"""Return a mock MQTT msg with a binary_sensor change message."""
sensor_json = json.loads(
await hass.async_add_executor_job(load_fixture, "zwave_mqtt/binary_sensor.json")
)
message = MQTTMessage(topic=sensor_json["topic"], payload=sensor_json["payload"])
message.encode()
return message
@pytest.fixture(name="binary_sensor_alt_msg")
async def binary_sensor_alt_msg_fixture(hass):
"""Return a mock MQTT msg with a binary_sensor change message."""
sensor_json = json.loads(
await hass.async_add_executor_job(
load_fixture, "zwave_mqtt/binary_sensor_alt.json"
)
)
message = MQTTMessage(topic=sensor_json["topic"], payload=sensor_json["payload"])
message.encode()
return message
@@ -0,0 +1,66 @@
"""Test Z-Wave Sensors."""
from homeassistant.components.binary_sensor import (
DEVICE_CLASS_MOTION,
DOMAIN as BINARY_SENSOR_DOMAIN,
)
from homeassistant.components.zwave_mqtt.const import DOMAIN
from homeassistant.const import ATTR_DEVICE_CLASS
from .common import setup_zwave
async def test_binary_sensor(hass, generic_data, binary_sensor_msg):
"""Test setting up config entry."""
receive_msg = await setup_zwave(hass, fixture=generic_data)
# Test Legacy sensor (disabled by default)
registry = await hass.helpers.entity_registry.async_get_registry()
entity_id = "binary_sensor.trisensor_sensor"
state = hass.states.get(entity_id)
assert state is None
entry = registry.async_get(entity_id)
assert entry
assert entry.disabled
assert entry.disabled_by == "integration"
# Test enabling legacy entity
updated_entry = registry.async_update_entity(
entry.entity_id, **{"disabled_by": None}
)
assert updated_entry != entry
assert updated_entry.disabled is False
# Test Sensor for Notification CC
state = hass.states.get("binary_sensor.trisensor_home_security_motion_detected")
assert state
assert state.state == "off"
assert state.attributes[ATTR_DEVICE_CLASS] == DEVICE_CLASS_MOTION
# Test incoming state change
receive_msg(binary_sensor_msg)
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.trisensor_home_security_motion_detected")
assert state.state == "on"
async def test_sensor_enabled(hass, generic_data, binary_sensor_alt_msg):
"""Test enabling a legacy binary_sensor."""
registry = await hass.helpers.entity_registry.async_get_registry()
entry = registry.async_get_or_create(
BINARY_SENSOR_DOMAIN,
DOMAIN,
"1-37-625737744",
suggested_object_id="trisensor_sensor_instance_1_sensor",
disabled_by=None,
)
assert entry.disabled is False
receive_msg = await setup_zwave(hass, fixture=generic_data)
receive_msg(binary_sensor_alt_msg)
await hass.async_block_till_done()
state = hass.states.get(entry.entity_id)
assert state is not None
assert state.state == "on"