Add setup type hints to nx584 (#63796)
Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
parent
0471a9e885
commit
bc2f4e82e3
2 changed files with 32 additions and 17 deletions
|
@ -1,4 +1,6 @@
|
|||
"""Support for exposing NX584 elements as sensors."""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import threading
|
||||
import time
|
||||
|
@ -14,7 +16,10 @@ from homeassistant.components.binary_sensor import (
|
|||
BinarySensorEntity,
|
||||
)
|
||||
from homeassistant.const import CONF_HOST, CONF_PORT
|
||||
from homeassistant.core import HomeAssistant
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -39,25 +44,30 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||
)
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
def setup_platform(
|
||||
hass: HomeAssistant,
|
||||
config: ConfigType,
|
||||
add_entities: AddEntitiesCallback,
|
||||
discovery_info: DiscoveryInfoType | None = None,
|
||||
) -> None:
|
||||
"""Set up the NX584 binary sensor platform."""
|
||||
|
||||
host = config.get(CONF_HOST)
|
||||
port = config.get(CONF_PORT)
|
||||
exclude = config.get(CONF_EXCLUDE_ZONES)
|
||||
zone_types = config.get(CONF_ZONE_TYPES)
|
||||
host = config[CONF_HOST]
|
||||
port = config[CONF_PORT]
|
||||
exclude = config[CONF_EXCLUDE_ZONES]
|
||||
zone_types = config[CONF_ZONE_TYPES]
|
||||
|
||||
try:
|
||||
client = nx584_client.Client(f"http://{host}:{port}")
|
||||
zones = client.list_zones()
|
||||
except requests.exceptions.ConnectionError as ex:
|
||||
_LOGGER.error("Unable to connect to NX584: %s", str(ex))
|
||||
return False
|
||||
return
|
||||
|
||||
version = [int(v) for v in client.get_version().split(".")]
|
||||
if version < [1, 1]:
|
||||
_LOGGER.error("NX584 is too old to use for sensors (>=0.2 required)")
|
||||
return False
|
||||
return
|
||||
|
||||
zone_sensors = {
|
||||
zone["number"]: NX584ZoneSensor(
|
||||
|
@ -72,7 +82,6 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||
watcher.start()
|
||||
else:
|
||||
_LOGGER.warning("No zones found on NX584")
|
||||
return True
|
||||
|
||||
|
||||
class NX584ZoneSensor(BinarySensorEntity):
|
||||
|
|
|
@ -8,6 +8,13 @@ import requests
|
|||
from homeassistant.components.nx584 import binary_sensor as nx584
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
DEFAULT_CONFIG = {
|
||||
"host": nx584.DEFAULT_HOST,
|
||||
"port": nx584.DEFAULT_PORT,
|
||||
"exclude_zones": [],
|
||||
"zone_types": {},
|
||||
}
|
||||
|
||||
|
||||
class StopMe(Exception):
|
||||
"""Stop helper."""
|
||||
|
@ -51,13 +58,8 @@ def client(fake_zones):
|
|||
def test_nx584_sensor_setup_defaults(mock_nx, mock_watcher, hass, fake_zones):
|
||||
"""Test the setup with no configuration."""
|
||||
add_entities = mock.MagicMock()
|
||||
config = {
|
||||
"host": nx584.DEFAULT_HOST,
|
||||
"port": nx584.DEFAULT_PORT,
|
||||
"exclude_zones": [],
|
||||
"zone_types": {},
|
||||
}
|
||||
assert nx584.setup_platform(hass, config, add_entities)
|
||||
config = DEFAULT_CONFIG
|
||||
nx584.setup_platform(hass, config, add_entities)
|
||||
mock_nx.assert_has_calls([mock.call(zone, "opening") for zone in fake_zones])
|
||||
assert add_entities.called
|
||||
assert nx584_client.Client.call_count == 1
|
||||
|
@ -76,7 +78,7 @@ def test_nx584_sensor_setup_full_config(mock_nx, mock_watcher, hass, fake_zones)
|
|||
"zone_types": {3: "motion"},
|
||||
}
|
||||
add_entities = mock.MagicMock()
|
||||
assert nx584.setup_platform(hass, config, add_entities)
|
||||
nx584.setup_platform(hass, config, add_entities)
|
||||
mock_nx.assert_has_calls(
|
||||
[
|
||||
mock.call(fake_zones[0], "opening"),
|
||||
|
@ -135,7 +137,11 @@ def test_nx584_sensor_setup_no_zones(hass):
|
|||
"""Test the setup with no zones."""
|
||||
nx584_client.Client.return_value.list_zones.return_value = []
|
||||
add_entities = mock.MagicMock()
|
||||
assert nx584.setup_platform(hass, {}, add_entities)
|
||||
nx584.setup_platform(
|
||||
hass,
|
||||
DEFAULT_CONFIG,
|
||||
add_entities,
|
||||
)
|
||||
assert not add_entities.called
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue