mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Migrate PVPC to has entity name (#98894)
* Migrate PVPC to has entity name * Set device name * Fix feedback
This commit is contained in:
parent
6223b1f599
commit
f28634ea11
@ -12,7 +12,7 @@ from homeassistant.components.sensor import (
|
|||||||
SensorStateClass,
|
SensorStateClass,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
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.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
@ -31,6 +31,7 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
|||||||
icon="mdi:currency-eur",
|
icon="mdi:currency-eur",
|
||||||
native_unit_of_measurement=f"{CURRENCY_EURO}/{UnitOfEnergy.KILO_WATT_HOUR}",
|
native_unit_of_measurement=f"{CURRENCY_EURO}/{UnitOfEnergy.KILO_WATT_HOUR}",
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
name="PVPC",
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
_PRICE_SENSOR_ATTRIBUTES_MAP = {
|
_PRICE_SENSOR_ATTRIBUTES_MAP = {
|
||||||
@ -118,34 +119,31 @@ async def async_setup_entry(
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the electricity price sensor from config_entry."""
|
"""Set up the electricity price sensor from config_entry."""
|
||||||
coordinator: ElecPricesDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
coordinator: ElecPricesDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||||
name = entry.data[CONF_NAME]
|
async_add_entities([ElecPriceSensor(coordinator, SENSOR_TYPES[0], entry.unique_id)])
|
||||||
async_add_entities(
|
|
||||||
[ElecPriceSensor(coordinator, SENSOR_TYPES[0], entry.unique_id, name)]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class ElecPriceSensor(CoordinatorEntity[ElecPricesDataUpdateCoordinator], SensorEntity):
|
class ElecPriceSensor(CoordinatorEntity[ElecPricesDataUpdateCoordinator], SensorEntity):
|
||||||
"""Class to hold the prices of electricity as a sensor."""
|
"""Class to hold the prices of electricity as a sensor."""
|
||||||
|
|
||||||
|
_attr_has_entity_name = True
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
coordinator: ElecPricesDataUpdateCoordinator,
|
coordinator: ElecPricesDataUpdateCoordinator,
|
||||||
description: SensorEntityDescription,
|
description: SensorEntityDescription,
|
||||||
unique_id: str | None,
|
unique_id: str | None,
|
||||||
name: str,
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize ESIOS sensor."""
|
"""Initialize ESIOS sensor."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
self.entity_description = description
|
self.entity_description = description
|
||||||
self._attr_attribution = coordinator.api.attribution
|
self._attr_attribution = coordinator.api.attribution
|
||||||
self._attr_unique_id = unique_id
|
self._attr_unique_id = unique_id
|
||||||
self._attr_name = name
|
|
||||||
self._attr_device_info = DeviceInfo(
|
self._attr_device_info = DeviceInfo(
|
||||||
configuration_url="https://api.esios.ree.es",
|
configuration_url="https://api.esios.ree.es",
|
||||||
entry_type=DeviceEntryType.SERVICE,
|
entry_type=DeviceEntryType.SERVICE,
|
||||||
identifiers={(DOMAIN, coordinator.entry_id)},
|
identifiers={(DOMAIN, coordinator.entry_id)},
|
||||||
manufacturer="REE",
|
manufacturer="REE",
|
||||||
name="ESIOS API",
|
name="ESIOS",
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_added_to_hass(self) -> None:
|
async def async_added_to_hass(self) -> None:
|
||||||
|
@ -57,7 +57,7 @@ async def test_config_flow(
|
|||||||
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
||||||
|
|
||||||
await hass.async_block_till_done()
|
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])
|
check_valid_state(state, tariff=TARIFFS[1])
|
||||||
assert pvpc_aioclient_mock.call_count == 1
|
assert pvpc_aioclient_mock.call_count == 1
|
||||||
|
|
||||||
@ -74,7 +74,7 @@ async def test_config_flow(
|
|||||||
|
|
||||||
# Check removal
|
# Check removal
|
||||||
registry = er.async_get(hass)
|
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)
|
assert await hass.config_entries.async_remove(registry_entity.config_entry_id)
|
||||||
|
|
||||||
# and add it again with UI
|
# and add it again with UI
|
||||||
@ -89,7 +89,7 @@ async def test_config_flow(
|
|||||||
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
||||||
|
|
||||||
await hass.async_block_till_done()
|
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])
|
check_valid_state(state, tariff=TARIFFS[1])
|
||||||
assert pvpc_aioclient_mock.call_count == 2
|
assert pvpc_aioclient_mock.call_count == 2
|
||||||
assert state.attributes["period"] == "P3"
|
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},
|
user_input={ATTR_POWER: 3.0, ATTR_POWER_P3: 4.6},
|
||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
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])
|
check_valid_state(state, tariff=TARIFFS[1])
|
||||||
assert pvpc_aioclient_mock.call_count == 3
|
assert pvpc_aioclient_mock.call_count == 3
|
||||||
assert state.attributes["period"] == "P3"
|
assert state.attributes["period"] == "P3"
|
||||||
@ -121,7 +121,7 @@ async def test_config_flow(
|
|||||||
freezer.tick(timedelta(days=1))
|
freezer.tick(timedelta(days=1))
|
||||||
async_fire_time_changed(hass)
|
async_fire_time_changed(hass)
|
||||||
await hass.async_block_till_done()
|
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")
|
check_valid_state(state, tariff=TARIFFS[0], value="unavailable")
|
||||||
assert "period" not in state.attributes
|
assert "period" not in state.attributes
|
||||||
assert pvpc_aioclient_mock.call_count == 4
|
assert pvpc_aioclient_mock.call_count == 4
|
||||||
|
Loading…
x
Reference in New Issue
Block a user