Use runtime_data in blue_current (#129084)

pull/129086/head
epenet 2024-10-24 15:46:31 +02:00 committed by GitHub
parent 86c37ce192
commit f63332a7aa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 16 deletions

View File

@ -22,6 +22,8 @@ from homeassistant.helpers.dispatcher import async_dispatcher_send
from .const import DOMAIN, EVSE_ID, LOGGER, MODEL_TYPE
type BlueCurrentConfigEntry = ConfigEntry[Connector]
PLATFORMS = [Platform.SENSOR]
CHARGE_POINTS = "CHARGE_POINTS"
DATA = "data"
@ -32,9 +34,10 @@ OBJECT = "object"
VALUE_TYPES = ["CH_STATUS"]
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
async def async_setup_entry(
hass: HomeAssistant, config_entry: BlueCurrentConfigEntry
) -> bool:
"""Set up Blue Current as a config entry."""
hass.data.setdefault(DOMAIN, {})
client = Client()
api_token = config_entry.data[CONF_API_TOKEN]
connector = Connector(hass, config_entry, client)
@ -50,29 +53,25 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
)
await client.wait_for_charge_points()
hass.data[DOMAIN][config_entry.entry_id] = connector
config_entry.runtime_data = connector
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
return True
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
async def async_unload_entry(
hass: HomeAssistant, config_entry: BlueCurrentConfigEntry
) -> bool:
"""Unload the Blue Current config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(
config_entry, PLATFORMS
)
if unload_ok:
hass.data[DOMAIN].pop(config_entry.entry_id)
return unload_ok
return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)
class Connector:
"""Define a class that connects to the Blue Current websocket API."""
def __init__(
self, hass: HomeAssistant, config: ConfigEntry, client: Client
self, hass: HomeAssistant, config: BlueCurrentConfigEntry, client: Client
) -> None:
"""Initialize."""
self.config = config

View File

@ -8,7 +8,6 @@ from homeassistant.components.sensor import (
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CURRENCY_EURO,
UnitOfElectricCurrent,
@ -19,7 +18,7 @@ from homeassistant.const import (
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import Connector
from . import BlueCurrentConfigEntry, Connector
from .const import DOMAIN
from .entity import BlueCurrentEntity, ChargepointEntity
@ -211,10 +210,12 @@ PARALLEL_UPDATES = 1
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
hass: HomeAssistant,
entry: BlueCurrentConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up Blue Current sensors."""
connector: Connector = hass.data[DOMAIN][entry.entry_id]
connector = entry.runtime_data
sensor_list: list[SensorEntity] = [
ChargePointSensor(connector, sensor, evse_id)
for evse_id in connector.charge_points