Add hour of free power select to Electric Kiwi (#97515)
* add select sensor to Electric Kiwi * Update homeassistant/components/electric_kiwi/select.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * simplify the HOP select since there is only one * remove handle coordinator state --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
parent
564e0110a4
commit
05e2acb091
4 changed files with 76 additions and 3 deletions
|
@ -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
|
||||
|
|
|
@ -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:
|
||||
|
|
69
homeassistant/components/electric_kiwi/select.py
Normal file
69
homeassistant/components/electric_kiwi/select.py
Normal file
|
@ -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()
|
|
@ -31,6 +31,11 @@
|
|||
"hopfreepowerend": {
|
||||
"name": "Hour of free power end"
|
||||
}
|
||||
},
|
||||
"select": {
|
||||
"hopselector": {
|
||||
"name": "Hour of free power"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue