hass-core/homeassistant/components/elmax/switch.py
Alberto Geniola b0affe7bfb
Elmax integration (#59321)
* Add elmax integration.

* Run hassfest and generate requirements_all

* Remove secondary platforms from elmax integration as per first component integration.

* Move ElmaxCoordinator and ElmaxEntity into external file
Linting review

* Remove useless variables

* Fix wrong indentation.

* Remove unecessary platforms.

* Remove unnecessary attributes from manifest.

* Rely on property getters/setters rathern than private attribute from parent.
Update internal entity state just after transitory state update.

* Update homeassistant/components/elmax/const.py

Reference Platform constant

Co-authored-by: Marvin Wichmann <marvin@fam-wichmann.de>

* Update username/password values

Rely on already-present templating constants

Co-authored-by: Marvin Wichmann <marvin@fam-wichmann.de>

* Add missing constant import.

* Remove unnecessary test_unhandled_error() callback implementation.

* Add common.py to coverage ignore list.

* Improve coverage of config_flow.

* Rename the integration.

Co-authored-by: Franck Nijhof <frenck@frenck.nl>

* Fix reauth bug and improve testing.

* Refactor lambdas into generators.

Co-authored-by: Marvin Wichmann <marvin@fam-wichmann.de>
Co-authored-by: Franck Nijhof <frenck@frenck.nl>
2021-12-07 22:42:55 +01:00

83 lines
3 KiB
Python

"""Elmax switch platform."""
from typing import Any
from elmax_api.model.command import SwitchCommand
from elmax_api.model.panel import PanelStatus
from homeassistant.components.elmax import ElmaxCoordinator
from homeassistant.components.elmax.common import ElmaxEntity
from homeassistant.components.elmax.const import DOMAIN
from homeassistant.components.switch import SwitchEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import HomeAssistantType
class ElmaxSwitch(ElmaxEntity, SwitchEntity):
"""Implement the Elmax switch entity."""
@property
def is_on(self) -> bool:
"""Return True if entity is on."""
if self.transitory_state is not None:
return self.transitory_state
return self._device.opened
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the entity on."""
client = self._coordinator.http_client
await client.execute_command(
endpoint_id=self._device.endpoint_id, command=SwitchCommand.TURN_ON
)
self.transitory_state = True
await self.async_update_ha_state()
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the entity off."""
client = self._coordinator.http_client
await client.execute_command(
endpoint_id=self._device.endpoint_id, command=SwitchCommand.TURN_OFF
)
self.transitory_state = False
await self.async_update_ha_state()
@property
def assumed_state(self) -> bool:
"""Return True if unable to access real state of the entity."""
return False
async def async_setup_entry(
hass: HomeAssistantType,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Elmax switch platform."""
coordinator: ElmaxCoordinator = hass.data[DOMAIN][config_entry.entry_id]
known_devices = set()
def _discover_new_devices():
panel_status = coordinator.panel_status # type: PanelStatus
# 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 actuator in panel_status.actuators:
entity = ElmaxSwitch(
panel=coordinator.panel_entry,
elmax_device=actuator,
panel_version=panel_status.release,
coordinator=coordinator,
)
if entity.unique_id not in known_devices:
entities.append(entity)
async_add_entities(entities, True)
known_devices.update([entity.unique_id for entity in entities])
# Register a listener for the discovery of new devices
coordinator.async_add_listener(_discover_new_devices)
# Immediately run a discovery, so we don't need to wait for the next update
_discover_new_devices()