mirror of
https://github.com/home-assistant/core.git
synced 2025-07-26 22:57:17 +00:00
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
@ -1,4 +1,6 @@
|
|||||||
"""Support for exposing NX584 elements as sensors."""
|
"""Support for exposing NX584 elements as sensors."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
@ -14,7 +16,10 @@ from homeassistant.components.binary_sensor import (
|
|||||||
BinarySensorEntity,
|
BinarySensorEntity,
|
||||||
)
|
)
|
||||||
from homeassistant.const import CONF_HOST, CONF_PORT
|
from homeassistant.const import CONF_HOST, CONF_PORT
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
import homeassistant.helpers.config_validation as cv
|
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__)
|
_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."""
|
"""Set up the NX584 binary sensor platform."""
|
||||||
|
|
||||||
host = config.get(CONF_HOST)
|
host = config[CONF_HOST]
|
||||||
port = config.get(CONF_PORT)
|
port = config[CONF_PORT]
|
||||||
exclude = config.get(CONF_EXCLUDE_ZONES)
|
exclude = config[CONF_EXCLUDE_ZONES]
|
||||||
zone_types = config.get(CONF_ZONE_TYPES)
|
zone_types = config[CONF_ZONE_TYPES]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
client = nx584_client.Client(f"http://{host}:{port}")
|
client = nx584_client.Client(f"http://{host}:{port}")
|
||||||
zones = client.list_zones()
|
zones = client.list_zones()
|
||||||
except requests.exceptions.ConnectionError as ex:
|
except requests.exceptions.ConnectionError as ex:
|
||||||
_LOGGER.error("Unable to connect to NX584: %s", str(ex))
|
_LOGGER.error("Unable to connect to NX584: %s", str(ex))
|
||||||
return False
|
return
|
||||||
|
|
||||||
version = [int(v) for v in client.get_version().split(".")]
|
version = [int(v) for v in client.get_version().split(".")]
|
||||||
if version < [1, 1]:
|
if version < [1, 1]:
|
||||||
_LOGGER.error("NX584 is too old to use for sensors (>=0.2 required)")
|
_LOGGER.error("NX584 is too old to use for sensors (>=0.2 required)")
|
||||||
return False
|
return
|
||||||
|
|
||||||
zone_sensors = {
|
zone_sensors = {
|
||||||
zone["number"]: NX584ZoneSensor(
|
zone["number"]: NX584ZoneSensor(
|
||||||
@ -72,7 +82,6 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
watcher.start()
|
watcher.start()
|
||||||
else:
|
else:
|
||||||
_LOGGER.warning("No zones found on NX584")
|
_LOGGER.warning("No zones found on NX584")
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
class NX584ZoneSensor(BinarySensorEntity):
|
class NX584ZoneSensor(BinarySensorEntity):
|
||||||
|
@ -8,6 +8,13 @@ import requests
|
|||||||
from homeassistant.components.nx584 import binary_sensor as nx584
|
from homeassistant.components.nx584 import binary_sensor as nx584
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
|
DEFAULT_CONFIG = {
|
||||||
|
"host": nx584.DEFAULT_HOST,
|
||||||
|
"port": nx584.DEFAULT_PORT,
|
||||||
|
"exclude_zones": [],
|
||||||
|
"zone_types": {},
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class StopMe(Exception):
|
class StopMe(Exception):
|
||||||
"""Stop helper."""
|
"""Stop helper."""
|
||||||
@ -51,13 +58,8 @@ def client(fake_zones):
|
|||||||
def test_nx584_sensor_setup_defaults(mock_nx, mock_watcher, hass, fake_zones):
|
def test_nx584_sensor_setup_defaults(mock_nx, mock_watcher, hass, fake_zones):
|
||||||
"""Test the setup with no configuration."""
|
"""Test the setup with no configuration."""
|
||||||
add_entities = mock.MagicMock()
|
add_entities = mock.MagicMock()
|
||||||
config = {
|
config = DEFAULT_CONFIG
|
||||||
"host": nx584.DEFAULT_HOST,
|
nx584.setup_platform(hass, config, add_entities)
|
||||||
"port": nx584.DEFAULT_PORT,
|
|
||||||
"exclude_zones": [],
|
|
||||||
"zone_types": {},
|
|
||||||
}
|
|
||||||
assert nx584.setup_platform(hass, config, add_entities)
|
|
||||||
mock_nx.assert_has_calls([mock.call(zone, "opening") for zone in fake_zones])
|
mock_nx.assert_has_calls([mock.call(zone, "opening") for zone in fake_zones])
|
||||||
assert add_entities.called
|
assert add_entities.called
|
||||||
assert nx584_client.Client.call_count == 1
|
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"},
|
"zone_types": {3: "motion"},
|
||||||
}
|
}
|
||||||
add_entities = mock.MagicMock()
|
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_nx.assert_has_calls(
|
||||||
[
|
[
|
||||||
mock.call(fake_zones[0], "opening"),
|
mock.call(fake_zones[0], "opening"),
|
||||||
@ -135,7 +137,11 @@ def test_nx584_sensor_setup_no_zones(hass):
|
|||||||
"""Test the setup with no zones."""
|
"""Test the setup with no zones."""
|
||||||
nx584_client.Client.return_value.list_zones.return_value = []
|
nx584_client.Client.return_value.list_zones.return_value = []
|
||||||
add_entities = mock.MagicMock()
|
add_entities = mock.MagicMock()
|
||||||
assert nx584.setup_platform(hass, {}, add_entities)
|
nx584.setup_platform(
|
||||||
|
hass,
|
||||||
|
DEFAULT_CONFIG,
|
||||||
|
add_entities,
|
||||||
|
)
|
||||||
assert not add_entities.called
|
assert not add_entities.called
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user