Skip to content

Commit

Permalink
Merge pull request #125 from mariusz-ostoja-swierczynski/feat_96
Browse files Browse the repository at this point in the history
✨ Add zone alarm state
  • Loading branch information
anarion80 authored Oct 20, 2024
2 parents 0d36ec2 + a2cf7f8 commit 62a2e8f
Show file tree
Hide file tree
Showing 19 changed files with 139 additions and 0 deletions.
1 change: 1 addition & 0 deletions custom_components/tech/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
INCLUDE_HUB_IN_NAME = "include_hub_in_name"
WINDOW_SENSORS = "windowsSensors"
WINDOW_STATE = "windowState"
ZONE_STATE = "zoneState"

DEFAULT_ICON = "mdi:eye"

Expand Down
87 changes: 87 additions & 0 deletions custom_components/tech/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
WINDOW_SENSORS,
WINDOW_STATE,
WORKING_STATUS,
ZONE_STATE,
)
from .coordinator import TechCoordinator
from .entity import TileEntity
Expand Down Expand Up @@ -136,6 +137,7 @@ async def async_setup_entry(

battery_devices = map_to_battery_sensors(zones, coordinator, config_entry)
temperature_sensors = map_to_temperature_sensors(zones, coordinator, config_entry)
zone_state_sensors = map_to_zone_state_sensors(zones, coordinator, config_entry)
humidity_sensors = map_to_humidity_sensors(zones, coordinator, config_entry)
actuator_sensors = map_to_actuator_sensors(zones, coordinator, config_entry)
window_sensors = map_to_window_sensors(zones, coordinator, config_entry)
Expand All @@ -148,6 +150,7 @@ async def async_setup_entry(
itertools.chain(
battery_devices,
temperature_sensors,
zone_state_sensors,
humidity_sensors, # , tile_sensors
actuator_sensors,
window_sensors,
Expand Down Expand Up @@ -227,6 +230,41 @@ def is_temperature_operating_device(device) -> bool:
return device[CONF_ZONE]["currentTemperature"] is not None


def map_to_zone_state_sensors(zones, coordinator, config_entry):
"""Map the zones to zone state sensors using the provided API and config entry.
Args:
zones (list): List of zones
coordinator (object): The API object
config_entry (object): The config entry object
model: device model
Returns:
list: List of ZoneStateSensor objects
"""
devices = filter(
lambda deviceIndex: is_zone_state_operating_device(zones[deviceIndex]), zones
)
return (
ZoneStateSensor(zones[deviceIndex], coordinator, config_entry)
for deviceIndex in devices
)


def is_zone_state_operating_device(device) -> bool:
"""Check if the device's current zone state is available.
Args:
device (dict): The device information.
Returns:
bool: True if the current zone state is available, False otherwise.
"""
return device[CONF_ZONE][ZONE_STATE] is not None


def map_to_humidity_sensors(zones, coordinator, config_entry):
"""Map zones to humidity sensors.
Expand Down Expand Up @@ -1166,6 +1204,55 @@ def update_properties(self, device):
self._attr_native_value = None


class ZoneStateSensor(BinarySensorEntity, ZoneSensor):
"""Representation of a Zone State (alarm) Sensor."""

_attr_device_class = BinarySensorDeviceClass.PROBLEM

def __init__(self, device, coordinator, config_entry) -> None:
"""Initialize the sensor.
These are needed before the call to super, as ZoneSensor class
calls update_properties in its init, which actually calls this class
update_properties, which does not know attrs and _window_index already.
"""
self._attr_is_on = device[CONF_ZONE][ZONE_STATE] != "noAlarm"
self.attrs: dict[str, Any] = {}
super().__init__(device, coordinator, config_entry)
self._attr_translation_key = "zone_state_entity"
self._attr_is_on = device[CONF_ZONE][ZONE_STATE] != "noAlarm"

@property
def unique_id(self) -> str:
"""Return a unique ID."""
return f"{self._unique_id}_zone_state"

@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the state attributes."""
attributes = {}
attributes.update(self.attrs)
return attributes

def update_properties(self, device):
"""Update the properties of the ZoneStateSensor object.
Args:
device (dict): The device information.
Returns:
None
"""
# Update the name of the device
self._name = device[CONF_DESCRIPTION][CONF_NAME]

self.attrs[ZONE_STATE] = device[CONF_ZONE][ZONE_STATE]

self._attr_is_on = device[CONF_ZONE][ZONE_STATE] != "noAlarm"


class TileSensor(TileEntity, CoordinatorEntity):
"""Representation of a TileSensor."""

Expand Down
3 changes: 3 additions & 0 deletions custom_components/tech/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@
},
"signal_strength_entity": {
"name": "{entity_name} signal strength"
},
"zone_state_entity": {
"name": "{entity_name} alarm state"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions custom_components/tech/translations/cs.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
},
"signal_strength_entity": {
"name": "{entity_name} Síla signálu"
},
"zone_state_entity": {
"name": "{entity_name} Stav alarmu"
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions custom_components/tech/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
},
"signal_strength_entity": {
"name": "{entity_name} Signalstärke"
},
"zone_state_entity": {
"name": "{entity_name} Alarmzustand"
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions custom_components/tech/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
},
"signal_strength_entity": {
"name": "{entity_name} Signal strength"
},
"zone_state_entity": {
"name": "{entity_name} Alarm state"
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions custom_components/tech/translations/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
},
"signal_strength_entity": {
"name": "Intensidad de señal de {entity_name}"
},
"zone_state_entity": {
"name": "{entity_name} Estado de alarma"
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions custom_components/tech/translations/et.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
},
"signal_strength_entity": {
"name": "{entity_name} Signaali tugevus"
},
"zone_state_entity": {
"name": "{entity_name} Häire olek"
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions custom_components/tech/translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
},
"signal_strength_entity": {
"name": "Force du signal {entity_name}"
},
"zone_state_entity": {
"name": "{entity_name} État d'alarme"
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions custom_components/tech/translations/hr.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
},
"signal_strength_entity": {
"name": "{entity_name} Jačina signala"
},
"zone_state_entity": {
"name": "{entity_name} Alarmno stanje"
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions custom_components/tech/translations/hu.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
},
"signal_strength_entity": {
"name": "{entity_name} Jel erősség"
},
"zone_state_entity": {
"name": "{entity_name} Riasztási állapot"
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions custom_components/tech/translations/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
},
"signal_strength_entity": {
"name": "{entity_name} Intensità del segnale"
},
"zone_state_entity": {
"name": "{entity_name} Stato di allarme"
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions custom_components/tech/translations/lt.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
},
"signal_strength_entity": {
"name": "{entity_name} Signalas"
},
"zone_state_entity": {
"name": "{entity_name} Signalizacijos būsena"
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions custom_components/tech/translations/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
},
"signal_strength_entity": {
"name": "{entity_name} Signaalsterkte"
},
"zone_state_entity": {
"name": "{entity_name} Alarmstatus"
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions custom_components/tech/translations/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
},
"signal_strength_entity": {
"name": "{entity_name} Siła sygnału"
},
"zone_state_entity": {
"name": "{entity_name} Stan alarmu"
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions custom_components/tech/translations/ro.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
},
"signal_strength_entity": {
"name": "{entity_name} Putere semnal"
},
"zone_state_entity": {
"name": "{entity_name} Stare de alarmă"
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions custom_components/tech/translations/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
},
"signal_strength_entity": {
"name": "{entity_name} Уровень сигнала"
},
"zone_state_entity": {
"name": "{entity_name} Состояние тревоги"
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions custom_components/tech/translations/si.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
},
"signal_strength_entity": {
"name": "{entity_name} Moč signala"
},
"zone_state_entity": {
"name": "{entity_name} Stanje alarma"
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions custom_components/tech/translations/sk.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
},
"signal_strength_entity": {
"name": "{entity_name} Intenzita signálu"
},
"zone_state_entity": {
"name": "{entity_name} Stav alarmu"
}
}
},
Expand Down

0 comments on commit 62a2e8f

Please sign in to comment.