switch.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. """Switch platform for Solar of Things integration."""
  2. from __future__ import annotations
  3. import logging
  4. from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
  5. from homeassistant.config_entries import ConfigEntry
  6. from homeassistant.core import HomeAssistant
  7. from homeassistant.helpers.entity_platform import AddEntitiesCallback
  8. from homeassistant.helpers.update_coordinator import CoordinatorEntity
  9. from .const import DOMAIN
  10. _LOGGER = logging.getLogger(__name__)
  11. async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback) -> None:
  12. data = hass.data[DOMAIN][entry.entry_id]
  13. api = data["api"]
  14. station_id: str = data["station_id"]
  15. device_coordinators = data["device_coordinators"]
  16. entities: list[SwitchEntity] = []
  17. for device_id, coordinator in device_coordinators.items():
  18. device_name = (coordinator.device_meta or {}).get("name") or device_id
  19. entities.extend(
  20. [
  21. SolarOfThingsGridChargingSwitch(api, coordinator, station_id, device_id, device_name),
  22. SolarOfThingsGridFeedInSwitch(api, coordinator, station_id, device_id, device_name),
  23. SolarOfThingsBackupModeSwitch(api, coordinator, station_id, device_id, device_name),
  24. ]
  25. )
  26. async_add_entities(entities)
  27. class _BaseSwitch(CoordinatorEntity, SwitchEntity):
  28. def __init__(self, api, coordinator, station_id: str, device_id: str, device_name: str) -> None:
  29. super().__init__(coordinator)
  30. self._api = api
  31. self._station_id = station_id
  32. self._device_id = device_id
  33. self._device_name = device_name
  34. @property
  35. def device_info(self):
  36. return {
  37. "identifiers": {(DOMAIN, self._station_id, self._device_id)},
  38. "name": self._device_name,
  39. "manufacturer": "Siseli",
  40. "model": (self.coordinator.data.get("device_meta") or {}).get("model") if self.coordinator.data else None,
  41. "via_device": (DOMAIN, self._station_id),
  42. }
  43. class SolarOfThingsGridChargingSwitch(_BaseSwitch):
  44. def __init__(self, api, coordinator, station_id: str, device_id: str, device_name: str) -> None:
  45. super().__init__(api, coordinator, station_id, device_id, device_name)
  46. self._attr_name = f"{device_name} Grid Charging"
  47. self._attr_unique_id = f"{DOMAIN}_{station_id}_{device_id}_grid_charging"
  48. self._attr_device_class = SwitchDeviceClass.SWITCH
  49. self._attr_icon = "mdi:transmission-tower"
  50. @property
  51. def is_on(self):
  52. return bool(((self.coordinator.data or {}).get("settings") or {}).get("gridChargingEnabled", False))
  53. async def async_turn_on(self, **kwargs):
  54. await self.hass.async_add_executor_job(self._api.set_grid_charging, self._device_id, True)
  55. await self.coordinator.async_request_refresh()
  56. async def async_turn_off(self, **kwargs):
  57. await self.hass.async_add_executor_job(self._api.set_grid_charging, self._device_id, False)
  58. await self.coordinator.async_request_refresh()
  59. class SolarOfThingsGridFeedInSwitch(_BaseSwitch):
  60. def __init__(self, api, coordinator, station_id: str, device_id: str, device_name: str) -> None:
  61. super().__init__(api, coordinator, station_id, device_id, device_name)
  62. self._attr_name = f"{device_name} Grid Feed-In"
  63. self._attr_unique_id = f"{DOMAIN}_{station_id}_{device_id}_grid_feed_in"
  64. self._attr_device_class = SwitchDeviceClass.SWITCH
  65. self._attr_icon = "mdi:transmission-tower-export"
  66. @property
  67. def is_on(self):
  68. return bool(((self.coordinator.data or {}).get("settings") or {}).get("gridFeedInEnabled", False))
  69. async def async_turn_on(self, **kwargs):
  70. await self.hass.async_add_executor_job(self._api.set_grid_feed_in, self._device_id, True)
  71. await self.coordinator.async_request_refresh()
  72. async def async_turn_off(self, **kwargs):
  73. await self.hass.async_add_executor_job(self._api.set_grid_feed_in, self._device_id, False)
  74. await self.coordinator.async_request_refresh()
  75. class SolarOfThingsBackupModeSwitch(_BaseSwitch):
  76. def __init__(self, api, coordinator, station_id: str, device_id: str, device_name: str) -> None:
  77. super().__init__(api, coordinator, station_id, device_id, device_name)
  78. self._attr_name = f"{device_name} Backup Mode"
  79. self._attr_unique_id = f"{DOMAIN}_{station_id}_{device_id}_backup_mode"
  80. self._attr_device_class = SwitchDeviceClass.SWITCH
  81. self._attr_icon = "mdi:battery-lock"
  82. @property
  83. def is_on(self):
  84. return bool(((self.coordinator.data or {}).get("settings") or {}).get("backupModeEnabled", False))
  85. async def async_turn_on(self, **kwargs):
  86. await self.hass.async_add_executor_job(self._api.set_backup_mode, self._device_id, True)
  87. await self.coordinator.async_request_refresh()
  88. async def async_turn_off(self, **kwargs):
  89. await self.hass.async_add_executor_job(self._api.set_backup_mode, self._device_id, False)
  90. await self.coordinator.async_request_refresh()