Migrate PVPC to has entity name (#98894)

* Migrate PVPC to has entity name

* Set device name

* Fix feedback
pull/99289/head
Joost Lekkerkerker 2023-08-29 16:26:23 +02:00 committed by GitHub
parent 6223b1f599
commit f28634ea11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 13 deletions

View File

@ -12,7 +12,7 @@ from homeassistant.components.sensor import (
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_NAME, CURRENCY_EURO, UnitOfEnergy
from homeassistant.const import CURRENCY_EURO, UnitOfEnergy
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -31,6 +31,7 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
icon="mdi:currency-eur",
native_unit_of_measurement=f"{CURRENCY_EURO}/{UnitOfEnergy.KILO_WATT_HOUR}",
state_class=SensorStateClass.MEASUREMENT,
name="PVPC",
),
)
_PRICE_SENSOR_ATTRIBUTES_MAP = {
@ -118,34 +119,31 @@ async def async_setup_entry(
) -> None:
"""Set up the electricity price sensor from config_entry."""
coordinator: ElecPricesDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
name = entry.data[CONF_NAME]
async_add_entities(
[ElecPriceSensor(coordinator, SENSOR_TYPES[0], entry.unique_id, name)]
)
async_add_entities([ElecPriceSensor(coordinator, SENSOR_TYPES[0], entry.unique_id)])
class ElecPriceSensor(CoordinatorEntity[ElecPricesDataUpdateCoordinator], SensorEntity):
"""Class to hold the prices of electricity as a sensor."""
_attr_has_entity_name = True
def __init__(
self,
coordinator: ElecPricesDataUpdateCoordinator,
description: SensorEntityDescription,
unique_id: str | None,
name: str,
) -> None:
"""Initialize ESIOS sensor."""
super().__init__(coordinator)
self.entity_description = description
self._attr_attribution = coordinator.api.attribution
self._attr_unique_id = unique_id
self._attr_name = name
self._attr_device_info = DeviceInfo(
configuration_url="https://api.esios.ree.es",
entry_type=DeviceEntryType.SERVICE,
identifiers={(DOMAIN, coordinator.entry_id)},
manufacturer="REE",
name="ESIOS API",
name="ESIOS",
)
async def async_added_to_hass(self) -> None:

View File

@ -57,7 +57,7 @@ async def test_config_flow(
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
await hass.async_block_till_done()
state = hass.states.get("sensor.test")
state = hass.states.get("sensor.esios_pvpc")
check_valid_state(state, tariff=TARIFFS[1])
assert pvpc_aioclient_mock.call_count == 1
@ -74,7 +74,7 @@ async def test_config_flow(
# Check removal
registry = er.async_get(hass)
registry_entity = registry.async_get("sensor.test")
registry_entity = registry.async_get("sensor.esios_pvpc")
assert await hass.config_entries.async_remove(registry_entity.config_entry_id)
# and add it again with UI
@ -89,7 +89,7 @@ async def test_config_flow(
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
await hass.async_block_till_done()
state = hass.states.get("sensor.test")
state = hass.states.get("sensor.esios_pvpc")
check_valid_state(state, tariff=TARIFFS[1])
assert pvpc_aioclient_mock.call_count == 2
assert state.attributes["period"] == "P3"
@ -110,7 +110,7 @@ async def test_config_flow(
user_input={ATTR_POWER: 3.0, ATTR_POWER_P3: 4.6},
)
await hass.async_block_till_done()
state = hass.states.get("sensor.test")
state = hass.states.get("sensor.esios_pvpc")
check_valid_state(state, tariff=TARIFFS[1])
assert pvpc_aioclient_mock.call_count == 3
assert state.attributes["period"] == "P3"
@ -121,7 +121,7 @@ async def test_config_flow(
freezer.tick(timedelta(days=1))
async_fire_time_changed(hass)
await hass.async_block_till_done()
state = hass.states.get("sensor.test")
state = hass.states.get("sensor.esios_pvpc")
check_valid_state(state, tariff=TARIFFS[0], value="unavailable")
assert "period" not in state.attributes
assert pvpc_aioclient_mock.call_count == 4