hass-core/homeassistant/components/elmax/binary_sensor.py
Alberto Geniola 86039de3cd
Add local API support to elmax (#94392)
* Add support for local (lan) panel integration

* Fix merge conflicts

* Remove executable flag from non-executable files

* Fix tests

* Update homeassistant/components/elmax/__init__.py

Shorten comment

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* Fix typehint

* Rename DummyPanel into DirectPanel

* Update homeassistant/components/elmax/__init__.py

Rewording

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* Update homeassistant/components/elmax/__init__.py

Rewording

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* Refactor option step into menu step

* Change requirement statement

* Refactor dictionary key entries to use existing constants

* Align step names to new constants

* Align step names to new constants amd align tests

* Align step names to new constants amd align tests

* Align step names to new constants

* Simplify logic to handle entire entry instead of a portion of the state

* Simplify working mode checks

* Add data_description dictionary to better explain SSL and FOLLOW_MDSN options

* Add support for local (lan) panel integration

* Fix merge conflicts

* Remove executable flag from non-executable files

* Fix tests

* Update homeassistant/components/elmax/__init__.py

Shorten comment

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* Fix typehint

* Rename DummyPanel into DirectPanel

* Update homeassistant/components/elmax/__init__.py

Rewording

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* Update homeassistant/components/elmax/__init__.py

Rewording

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* Refactor option step into menu step

* Change requirement statement

* Refactor dictionary key entries to use existing constants

* Align step names to new constants

* Align step names to new constants amd align tests

* Align step names to new constants amd align tests

* Align step names to new constants

* Simplify logic to handle entire entry instead of a portion of the state

* Simplify working mode checks

* Add data_description dictionary to better explain SSL and FOLLOW_MDSN options

* Add newline at end of file

* Remove CONF_ELMAX_MODE_DIRECT_FOLLOW_MDNS option

* Fix Ruff pre-check

---------

Co-authored-by: Erik Montnemery <erik@montnemery.com>
2024-03-04 11:39:13 +01:00

67 lines
2.1 KiB
Python

"""Elmax sensor platform."""
from __future__ import annotations
from elmax_api.model.panel import PanelStatus
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import ElmaxCoordinator
from .common import ElmaxEntity
from .const import DOMAIN
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Elmax sensor platform."""
coordinator: ElmaxCoordinator = hass.data[DOMAIN][config_entry.entry_id]
known_devices = set()
def _discover_new_devices():
panel_status: PanelStatus = coordinator.data
# In case the panel is offline, its status will be None. In that case, simply do nothing
if panel_status is None:
return
# Otherwise, add all the entities we found
entities = []
for zone in panel_status.zones:
# Skip already handled devices
if zone.endpoint_id in known_devices:
continue
entity = ElmaxSensor(
elmax_device=zone,
panel_version=panel_status.release,
coordinator=coordinator,
)
entities.append(entity)
if entities:
async_add_entities(entities)
known_devices.update([e.unique_id for e in entities])
# Register a listener for the discovery of new devices
remove_handle = coordinator.async_add_listener(_discover_new_devices)
config_entry.async_on_unload(remove_handle)
# Immediately run a discovery, so we don't need to wait for the next update
_discover_new_devices()
class ElmaxSensor(ElmaxEntity, BinarySensorEntity):
"""Elmax Sensor entity implementation."""
_attr_device_class = BinarySensorDeviceClass.DOOR
@property
def is_on(self) -> bool:
"""Return true if the binary sensor is on."""
return self.coordinator.get_zone_state(self._device.endpoint_id).opened