1 """Support for exposing Concord232 elements as sensors."""
3 from __future__
import annotations
8 from concord232
import client
as concord232_client
10 import voluptuous
as vol
13 DEVICE_CLASSES_SCHEMA
as BINARY_SENSOR_DEVICE_CLASSES_SCHEMA,
14 PLATFORM_SCHEMA
as BINARY_SENSOR_PLATFORM_SCHEMA,
15 BinarySensorDeviceClass,
25 _LOGGER = logging.getLogger(__name__)
27 CONF_EXCLUDE_ZONES =
"exclude_zones"
28 CONF_ZONE_TYPES =
"zone_types"
30 DEFAULT_HOST =
"localhost"
31 DEFAULT_NAME =
"Alarm"
35 SCAN_INTERVAL = datetime.timedelta(seconds=10)
37 ZONE_TYPES_SCHEMA = vol.Schema({cv.positive_int: BINARY_SENSOR_DEVICE_CLASSES_SCHEMA})
39 PLATFORM_SCHEMA = BINARY_SENSOR_PLATFORM_SCHEMA.extend(
41 vol.Optional(CONF_EXCLUDE_ZONES, default=[]): vol.All(
42 cv.ensure_list, [cv.positive_int]
44 vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string,
45 vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
46 vol.Optional(CONF_ZONE_TYPES, default={}): ZONE_TYPES_SCHEMA,
54 add_entities: AddEntitiesCallback,
55 discovery_info: DiscoveryInfoType |
None =
None,
57 """Set up the Concord232 binary sensor platform."""
59 host = config[CONF_HOST]
60 port = config[CONF_PORT]
61 exclude = config[CONF_EXCLUDE_ZONES]
62 zone_types = config[CONF_ZONE_TYPES]
66 _LOGGER.debug(
"Initializing client")
67 client = concord232_client.Client(f
"http://{host}:{port}")
68 client.zones = client.list_zones()
69 client.last_zone_update = dt_util.utcnow()
71 except requests.exceptions.ConnectionError
as ex:
72 _LOGGER.error(
"Unable to connect to Concord232: %s",
str(ex))
80 client.zones.sort(key=
lambda zone: zone[
"number"])
82 for zone
in client.zones:
83 _LOGGER.debug(
"Loading Zone found: %s", zone[
"name"])
84 if zone[
"number"]
not in exclude:
98 """Return the result of the type guessing from name."""
99 if "MOTION" in zone[
"name"]:
100 return BinarySensorDeviceClass.MOTION
101 if "KEY" in zone[
"name"]:
102 return BinarySensorDeviceClass.SAFETY
103 if "SMOKE" in zone[
"name"]:
104 return BinarySensorDeviceClass.SMOKE
105 if "WATER" in zone[
"name"]:
107 return BinarySensorDeviceClass.OPENING
111 """Representation of a Concord232 zone as a sensor."""
114 """Initialize the Concord232 binary sensor."""
123 """Return the class of this sensor, from DEVICE_CLASSES."""
128 """Return the name of the binary sensor."""
129 return self.
_zone_zone[
"name"]
133 """Return true if the binary sensor is on."""
135 return bool(self.
_zone_zone[
"state"] !=
"Normal")
138 """Get updated stats from API."""
139 last_update = dt_util.utcnow() - self.
_client_client.last_zone_update
140 _LOGGER.debug(
"Zone: %s ", self.
_zone_zone)
141 if last_update > datetime.timedelta(seconds=1):
143 self.
_client_client.last_zone_update = dt_util.utcnow()
144 _LOGGER.debug(
"Updated from zone: %s", self.
_zone_zone[
"name"])
146 if hasattr(self.
_client_client,
"zones"):
147 self.
_zone_zone = next(
148 (x
for x
in self.
_client_client.zones
if x[
"number"] == self.
_number_number),
None
def __init__(self, hass, client, zone, zone_type)
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
def get_opening_type(zone)