1 """Control switches."""
4 from datetime
import timedelta
8 from ProgettiHWSW.relay
import Relay
16 DataUpdateCoordinator,
19 from .
import setup_switch
20 from .const
import DEFAULT_POLLING_INTERVAL_SEC, DOMAIN
22 _LOGGER = logging.getLogger(DOMAIN)
27 config_entry: ConfigEntry,
28 async_add_entities: AddEntitiesCallback,
30 """Set up the switches from a config entry."""
31 board_api = hass.data[DOMAIN][config_entry.entry_id]
32 relay_count = config_entry.data[
"relay_count"]
34 async
def async_update_data():
35 """Fetch data from API endpoint of board."""
36 async
with asyncio.timeout(5):
37 return await board_api.get_switches()
43 update_method=async_update_data,
44 update_interval=
timedelta(seconds=DEFAULT_POLLING_INTERVAL_SEC),
46 await coordinator.async_refresh()
52 setup_switch(board_api, i, config_entry.data[f
"relay_{i!s}"]),
54 for i
in range(1,
int(relay_count) + 1)
59 """Represent a switch entity."""
61 def __init__(self, coordinator, name, switch: Relay) ->
None:
62 """Initialize the values."""
68 """Turn the switch on."""
69 await self.
_switch_switch.control(
True)
70 await self.coordinator.async_request_refresh()
73 """Turn the switch off."""
74 await self.
_switch_switch.control(
False)
75 await self.coordinator.async_request_refresh()
78 """Toggle the state of switch."""
80 await self.coordinator.async_request_refresh()
84 """Get switch state."""
85 return self.coordinator.data[self.
_switch_switch.id]
None __init__(self, coordinator, name, Relay switch)
None async_turn_off(self, **Any kwargs)
None async_toggle(self, **Any kwargs)
None async_turn_on(self, **Any kwargs)
None toggle(self, **Any kwargs)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Relay setup_switch(ProgettiHWSWAPI api, int switch_number, str mode)