Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Breeze switch of the Renson ventilation unit."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from renson_endura_delta.field_enum import CURRENT_LEVEL_FIELD, DataType
9 from renson_endura_delta.renson import Level, RensonVentilation
10 
11 from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
12 from homeassistant.config_entries import ConfigEntry
13 from homeassistant.core import HomeAssistant, callback
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 
16 from . import RensonCoordinator
17 from .const import DOMAIN
18 from .entity import RensonEntity
19 
20 _LOGGER = logging.getLogger(__name__)
21 
22 
24  """Provide the breeze switch."""
25 
26  _attr_device_class = SwitchDeviceClass.SWITCH
27  _attr_has_entity_name = True
28  _attr_translation_key = "breeze"
29 
30  def __init__(
31  self,
32  api: RensonVentilation,
33  coordinator: RensonCoordinator,
34  ) -> None:
35  """Initialize class."""
36  super().__init__("breeze", api, coordinator)
37 
38  self._attr_is_on_attr_is_on = False
39 
40  async def async_turn_on(self, **kwargs: Any) -> None:
41  """Turn on the switch."""
42  _LOGGER.debug("Enable Breeze")
43 
44  await self.hasshasshass.async_add_executor_job(self.apiapiapi.set_manual_level, Level.BREEZE)
45  await self.coordinator.async_request_refresh()
46 
47  async def async_turn_off(self, **kwargs: Any) -> None:
48  """Turn off the switch."""
49  _LOGGER.debug("Disable Breeze")
50 
51  await self.hasshasshass.async_add_executor_job(self.apiapiapi.set_manual_level, Level.OFF)
52  await self.coordinator.async_request_refresh()
53 
54  @callback
55  def _handle_coordinator_update(self) -> None:
56  """Handle updated data from the coordinator."""
57 
58  level = self.apiapiapi.parse_value(
59  self.apiapiapi.get_field_value(self.coordinator.data, CURRENT_LEVEL_FIELD.name),
60  DataType.LEVEL,
61  )
62 
63  self._attr_is_on_attr_is_on = level == Level.BREEZE.value
64 
65  self.async_write_ha_stateasync_write_ha_state()
66 
67 
69  hass: HomeAssistant,
70  config_entry: ConfigEntry,
71  async_add_entities: AddEntitiesCallback,
72 ) -> None:
73  """Call the Renson integration to setup."""
74 
75  api: RensonVentilation = hass.data[DOMAIN][config_entry.entry_id].api
76  coordinator: RensonCoordinator = hass.data[DOMAIN][
77  config_entry.entry_id
78  ].coordinator
79 
80  async_add_entities([RensonBreezeSwitch(api, coordinator)])
None __init__(self, RensonVentilation api, RensonCoordinator coordinator)
Definition: switch.py:34
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:72