diff --git a/.coveragerc b/.coveragerc index fb1869b2489..9c6e7a1a223 100644 --- a/.coveragerc +++ b/.coveragerc @@ -270,6 +270,7 @@ omit = homeassistant/components/electric_kiwi/oauth2.py homeassistant/components/electric_kiwi/sensor.py homeassistant/components/electric_kiwi/coordinator.py + homeassistant/components/electric_kiwi/select.py homeassistant/components/eliqonline/sensor.py homeassistant/components/elkm1/__init__.py homeassistant/components/elkm1/alarm_control_panel.py diff --git a/homeassistant/components/electric_kiwi/__init__.py b/homeassistant/components/electric_kiwi/__init__.py index 3ae6b1c70cf..5af02f69bcf 100644 --- a/homeassistant/components/electric_kiwi/__init__.py +++ b/homeassistant/components/electric_kiwi/__init__.py @@ -15,9 +15,7 @@ from . import api from .const import DOMAIN from .coordinator import ElectricKiwiHOPDataCoordinator -PLATFORMS: list[Platform] = [ - Platform.SENSOR, -] +PLATFORMS: list[Platform] = [Platform.SENSOR, Platform.SELECT] async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: diff --git a/homeassistant/components/electric_kiwi/select.py b/homeassistant/components/electric_kiwi/select.py new file mode 100644 index 00000000000..a474c315258 --- /dev/null +++ b/homeassistant/components/electric_kiwi/select.py @@ -0,0 +1,69 @@ +"""Support for Electric Kiwi hour of free power.""" +from __future__ import annotations + +import logging + +from homeassistant.components.select import SelectEntity, SelectEntityDescription +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import EntityCategory +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.update_coordinator import CoordinatorEntity + +from .const import ATTRIBUTION, DOMAIN +from .coordinator import ElectricKiwiHOPDataCoordinator + +_LOGGER = logging.getLogger(__name__) +ATTR_EK_HOP_SELECT = "hop_select" + +HOP_SELECT = SelectEntityDescription( + entity_category=EntityCategory.CONFIG, + key=ATTR_EK_HOP_SELECT, + translation_key="hopselector", +) + + +async def async_setup_entry( + hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback +) -> None: + """Electric Kiwi select setup.""" + hop_coordinator: ElectricKiwiHOPDataCoordinator = hass.data[DOMAIN][entry.entry_id] + + _LOGGER.debug("Setting up HOP entity") + entities = [ElectricKiwiSelectHOPEntity(hop_coordinator, HOP_SELECT)] + async_add_entities(entities) + + +class ElectricKiwiSelectHOPEntity( + CoordinatorEntity[ElectricKiwiHOPDataCoordinator], SelectEntity +): + """Entity object for seeing and setting the hour of free power.""" + + entity_description: SelectEntityDescription + _attr_has_entity_name = True + _attr_attribution = ATTRIBUTION + values_dict: dict[str, int] + + def __init__( + self, + hop_coordinator: ElectricKiwiHOPDataCoordinator, + description: SelectEntityDescription, + ) -> None: + """Initialise the HOP selection entity.""" + super().__init__(hop_coordinator) + self._attr_unique_id = f"{self.coordinator._ek_api.customer_number}_{self.coordinator._ek_api.connection_id}_{description.key}" + self.entity_description = description + self._state = None + self.values_dict = self.coordinator.get_hop_options() + self._attr_options = list(self.values_dict.keys()) + + @property + def current_option(self) -> str | None: + """Return the currently selected option.""" + return f"{self.coordinator.data.start.start_time} - {self.coordinator.data.end.end_time}" + + async def async_select_option(self, option: str) -> None: + """Change the selected option.""" + value = self.values_dict[option] + await self.coordinator.async_update_hop(value) + self.async_write_ha_state() diff --git a/homeassistant/components/electric_kiwi/strings.json b/homeassistant/components/electric_kiwi/strings.json index 19056180f17..81de5cef896 100644 --- a/homeassistant/components/electric_kiwi/strings.json +++ b/homeassistant/components/electric_kiwi/strings.json @@ -31,6 +31,11 @@ "hopfreepowerend": { "name": "Hour of free power end" } + }, + "select": { + "hopselector": { + "name": "Hour of free power" + } } } }