diff --git a/homeassistant/components/pvpc_hourly_pricing/sensor.py b/homeassistant/components/pvpc_hourly_pricing/sensor.py index 73881d16a4b..3368b24b3ff 100644 --- a/homeassistant/components/pvpc_hourly_pricing/sensor.py +++ b/homeassistant/components/pvpc_hourly_pricing/sensor.py @@ -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: diff --git a/tests/components/pvpc_hourly_pricing/test_config_flow.py b/tests/components/pvpc_hourly_pricing/test_config_flow.py index e22ab03eb60..6560c81ebbb 100644 --- a/tests/components/pvpc_hourly_pricing/test_config_flow.py +++ b/tests/components/pvpc_hourly_pricing/test_config_flow.py @@ -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