-
-
Notifications
You must be signed in to change notification settings - Fork 28.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add diagnostic sensors for TotalConnect (#73152)
* add diagnostic sensors * test binary_sensor.py file * add tests for binary sensor * fix zone type checks and error on unknown * improve entity tests * hide entities by default * Revert "hide entities by default" This reverts commit 9808d73. * Update homeassistant/components/totalconnect/binary_sensor.py Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io> * update binary_sensor per comments * update test * move to _attr_extra_state_attributes * no spaces in unique_id * update per balloob suggestions * fix typing * fix black and mypy * Apply suggestions from code review Co-authored-by: G Johansson <goran.johansson@shiftit.se> * add more to binary_sensor tests * remove unused import --------- Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io> Co-authored-by: G Johansson <goran.johansson@shiftit.se>
- Loading branch information
1 parent
bf6d429
commit 16c9158
Showing
4 changed files
with
273 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
"""Tests for the TotalConnect binary sensor.""" | ||
from unittest.mock import patch | ||
|
||
from homeassistant.components.binary_sensor import ( | ||
DOMAIN as BINARY_SENSOR, | ||
BinarySensorDeviceClass, | ||
) | ||
from homeassistant.const import ATTR_FRIENDLY_NAME, STATE_OFF, STATE_ON | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers import entity_registry as er | ||
|
||
from .common import LOCATION_ID, RESPONSE_DISARMED, ZONE_NORMAL, setup_platform | ||
|
||
ZONE_ENTITY_ID = "binary_sensor.security" | ||
ZONE_LOW_BATTERY_ID = "binary_sensor.security_low_battery" | ||
ZONE_TAMPER_ID = "binary_sensor.security_tamper" | ||
PANEL_BATTERY_ID = "binary_sensor.test_low_battery" | ||
PANEL_TAMPER_ID = "binary_sensor.test_tamper" | ||
PANEL_POWER_ID = "binary_sensor.test_power" | ||
|
||
|
||
async def test_entity_registry(hass: HomeAssistant) -> None: | ||
"""Test the binary sensor is registered in entity registry.""" | ||
await setup_platform(hass, BINARY_SENSOR) | ||
entity_registry = er.async_get(hass) | ||
|
||
# ensure zone 1 plus two diagnostic zones are created | ||
entry = entity_registry.async_get(ZONE_ENTITY_ID) | ||
entry_low_battery = entity_registry.async_get(ZONE_LOW_BATTERY_ID) | ||
entry_tamper = entity_registry.async_get(ZONE_TAMPER_ID) | ||
|
||
assert entry.unique_id == f"{LOCATION_ID}_{ZONE_NORMAL['ZoneID']}_zone" | ||
assert ( | ||
entry_low_battery.unique_id | ||
== f"{LOCATION_ID}_{ZONE_NORMAL['ZoneID']}_low_battery" | ||
) | ||
assert entry_tamper.unique_id == f"{LOCATION_ID}_{ZONE_NORMAL['ZoneID']}_tamper" | ||
|
||
# ensure panel diagnostic zones are created | ||
panel_battery = entity_registry.async_get(PANEL_BATTERY_ID) | ||
panel_tamper = entity_registry.async_get(PANEL_TAMPER_ID) | ||
panel_power = entity_registry.async_get(PANEL_POWER_ID) | ||
|
||
assert panel_battery.unique_id == f"{LOCATION_ID}_low_battery" | ||
assert panel_tamper.unique_id == f"{LOCATION_ID}_tamper" | ||
assert panel_power.unique_id == f"{LOCATION_ID}_power" | ||
|
||
|
||
async def test_state_and_attributes(hass: HomeAssistant) -> None: | ||
"""Test the binary sensor attributes are correct.""" | ||
|
||
with patch( | ||
"homeassistant.components.totalconnect.TotalConnectClient.request", | ||
return_value=RESPONSE_DISARMED, | ||
): | ||
await setup_platform(hass, BINARY_SENSOR) | ||
|
||
state = hass.states.get(ZONE_ENTITY_ID) | ||
assert state.state == STATE_ON | ||
assert ( | ||
state.attributes.get(ATTR_FRIENDLY_NAME) == ZONE_NORMAL["ZoneDescription"] | ||
) | ||
assert state.attributes.get("device_class") == BinarySensorDeviceClass.DOOR | ||
|
||
state = hass.states.get(f"{ZONE_ENTITY_ID}_low_battery") | ||
assert state.state == STATE_OFF | ||
state = hass.states.get(f"{ZONE_ENTITY_ID}_tamper") | ||
assert state.state == STATE_OFF | ||
|
||
# Zone 2 is fire with low battery | ||
state = hass.states.get("binary_sensor.fire") | ||
assert state.state == STATE_OFF | ||
assert state.attributes.get("device_class") == BinarySensorDeviceClass.SMOKE | ||
state = hass.states.get("binary_sensor.fire_low_battery") | ||
assert state.state == STATE_ON | ||
state = hass.states.get("binary_sensor.fire_tamper") | ||
assert state.state == STATE_OFF | ||
|
||
# Zone 3 is gas with tamper | ||
state = hass.states.get("binary_sensor.gas") | ||
assert state.state == STATE_OFF | ||
assert state.attributes.get("device_class") == BinarySensorDeviceClass.GAS | ||
state = hass.states.get("binary_sensor.gas_low_battery") | ||
assert state.state == STATE_OFF | ||
state = hass.states.get("binary_sensor.gas_tamper") | ||
assert state.state == STATE_ON |