Files
core/homeassistant/components/blue_current/entity.py
Nick Kuiper 4b7650f2d2 Add buttons to Blue current integration (#143964)
* Add buttons to Blue current integration

* Apply feedback

* Changed configEntry to use the BlueCurrentConfigEntry.

The connector is now accessed via the entry instead of hass.data.

* Changed test_buttons_created test to use the snapshot_platform function.

Also removed the entry.unique_id check in the test_charge_point_buttons function because this is not needed anymore, according to https://github.com/home-assistant/core/pull/114000#discussion_r1627201872

* Applied requested changes.

Changes requested by joostlek.

* Moved has_value from BlueCurrentEntity to class level.

This value was still inside the __init__ function, so the value was not overwritten by the ChargePointButton.

---------

Co-authored-by: Floris272 <florispuijk@outlook.com>
2025-05-14 19:37:16 +02:00

64 lines
2.0 KiB
Python

"""Entity representing a Blue Current charge point."""
from homeassistant.const import ATTR_NAME
from homeassistant.core import callback
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import Entity
from . import Connector
from .const import DOMAIN, MODEL_TYPE
class BlueCurrentEntity(Entity):
"""Define a base Blue Current entity."""
_attr_has_entity_name = True
_attr_should_poll = False
has_value = False
def __init__(self, connector: Connector, signal: str) -> None:
"""Initialize the entity."""
self.connector = connector
self.signal = signal
async def async_added_to_hass(self) -> None:
"""Register callbacks."""
@callback
def update() -> None:
"""Update the state."""
self.update_from_latest_data()
self.async_write_ha_state()
self.async_on_remove(async_dispatcher_connect(self.hass, self.signal, update))
self.update_from_latest_data()
@property
def available(self) -> bool:
"""Return entity availability."""
return self.connector.connected and self.has_value
@callback
def update_from_latest_data(self) -> None:
"""Update the entity from the latest data."""
class ChargepointEntity(BlueCurrentEntity):
"""Define a base charge point entity."""
def __init__(self, connector: Connector, evse_id: str) -> None:
"""Initialize the entity."""
super().__init__(connector, f"{DOMAIN}_charge_point_update_{evse_id}")
chargepoint_name = connector.charge_points[evse_id][ATTR_NAME]
self.evse_id = evse_id
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, evse_id)},
name=chargepoint_name if chargepoint_name != "" else evse_id,
manufacturer="Blue Current",
model=connector.charge_points[evse_id][MODEL_TYPE],
)