Move point base entity to separate module (#126551)

This commit is contained in:
epenet 2024-09-23 15:58:20 +02:00 committed by GitHub
parent 02ab2c1433
commit f5852b4678
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 98 additions and 92 deletions

View File

@ -27,18 +27,11 @@ from homeassistant.helpers import (
aiohttp_client,
config_entry_oauth2_flow,
config_validation as cv,
device_registry as dr,
)
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.dispatcher import (
async_dispatcher_connect,
async_dispatcher_send,
)
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.event import async_track_time_interval
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from homeassistant.helpers.typing import ConfigType
from homeassistant.util.dt import as_local, parse_datetime, utc_from_timestamp
from . import api
from .const import (
@ -284,88 +277,6 @@ class MinutPointClient:
return await self._client.alarm_arm(home_id)
class MinutPointEntity(Entity): # pylint: disable=hass-enforce-class-module # see PR 118243
"""Base Entity used by the sensors."""
_attr_should_poll = False
def __init__(self, point_client, device_id, device_class) -> None:
"""Initialize the entity."""
self._async_unsub_dispatcher_connect = None
self._client = point_client
self._id = device_id
self._name = self.device.name
self._attr_device_class = device_class
self._updated = utc_from_timestamp(0)
self._attr_unique_id = f"point.{device_id}-{device_class}"
device = self.device.device
self._attr_device_info = DeviceInfo(
connections={(dr.CONNECTION_NETWORK_MAC, device["device_mac"])},
identifiers={(DOMAIN, device["device_id"])},
manufacturer="Minut",
model=f"Point v{device['hardware_version']}",
name=device["description"],
sw_version=device["firmware"]["installed"],
via_device=(DOMAIN, device["home"]),
)
if device_class:
self._attr_name = f"{self._name} {device_class.capitalize()}"
def __str__(self) -> str:
"""Return string representation of device."""
return f"MinutPoint {self.name}"
async def async_added_to_hass(self):
"""Call when entity is added to hass."""
_LOGGER.debug("Created device %s", self)
self._async_unsub_dispatcher_connect = async_dispatcher_connect(
self.hass, SIGNAL_UPDATE_ENTITY, self._update_callback
)
await self._update_callback()
async def async_will_remove_from_hass(self):
"""Disconnect dispatcher listener when removed."""
if self._async_unsub_dispatcher_connect:
self._async_unsub_dispatcher_connect()
async def _update_callback(self):
"""Update the value of the sensor."""
@property
def available(self):
"""Return true if device is not offline."""
return self._client.is_available(self.device_id)
@property
def device(self):
"""Return the representation of the device."""
return self._client.device(self.device_id)
@property
def device_id(self):
"""Return the id of the device."""
return self._id
@property
def extra_state_attributes(self):
"""Return status of device."""
attrs = self.device.device_status
attrs["last_heard_from"] = as_local(self.last_update).strftime(
"%Y-%m-%d %H:%M:%S"
)
return attrs
@property
def is_updated(self):
"""Return true if sensor have been updated."""
return self.last_update > self._updated
@property
def last_update(self):
"""Return the last_update time for the device."""
return parse_datetime(self.device.last_update)
@dataclass
class PointData:
"""Point Data."""

View File

@ -16,8 +16,8 @@ from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import MinutPointEntity
from .const import DOMAIN as POINT_DOMAIN, POINT_DISCOVERY_NEW, SIGNAL_WEBHOOK
from .entity import MinutPointEntity
_LOGGER = logging.getLogger(__name__)

View File

@ -0,0 +1,95 @@
"""Support for Minut Point."""
import logging
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import Entity
from homeassistant.util.dt import as_local, parse_datetime, utc_from_timestamp
from .const import DOMAIN, SIGNAL_UPDATE_ENTITY
_LOGGER = logging.getLogger(__name__)
class MinutPointEntity(Entity):
"""Base Entity used by the sensors."""
_attr_should_poll = False
def __init__(self, point_client, device_id, device_class) -> None:
"""Initialize the entity."""
self._async_unsub_dispatcher_connect = None
self._client = point_client
self._id = device_id
self._name = self.device.name
self._attr_device_class = device_class
self._updated = utc_from_timestamp(0)
self._attr_unique_id = f"point.{device_id}-{device_class}"
device = self.device.device
self._attr_device_info = DeviceInfo(
connections={(dr.CONNECTION_NETWORK_MAC, device["device_mac"])},
identifiers={(DOMAIN, device["device_id"])},
manufacturer="Minut",
model=f"Point v{device['hardware_version']}",
name=device["description"],
sw_version=device["firmware"]["installed"],
via_device=(DOMAIN, device["home"]),
)
if device_class:
self._attr_name = f"{self._name} {device_class.capitalize()}"
def __str__(self) -> str:
"""Return string representation of device."""
return f"MinutPoint {self.name}"
async def async_added_to_hass(self):
"""Call when entity is added to hass."""
_LOGGER.debug("Created device %s", self)
self._async_unsub_dispatcher_connect = async_dispatcher_connect(
self.hass, SIGNAL_UPDATE_ENTITY, self._update_callback
)
await self._update_callback()
async def async_will_remove_from_hass(self):
"""Disconnect dispatcher listener when removed."""
if self._async_unsub_dispatcher_connect:
self._async_unsub_dispatcher_connect()
async def _update_callback(self):
"""Update the value of the sensor."""
@property
def available(self):
"""Return true if device is not offline."""
return self._client.is_available(self.device_id)
@property
def device(self):
"""Return the representation of the device."""
return self._client.device(self.device_id)
@property
def device_id(self):
"""Return the id of the device."""
return self._id
@property
def extra_state_attributes(self):
"""Return status of device."""
attrs = self.device.device_status
attrs["last_heard_from"] = as_local(self.last_update).strftime(
"%Y-%m-%d %H:%M:%S"
)
return attrs
@property
def is_updated(self):
"""Return true if sensor have been updated."""
return self.last_update > self._updated
@property
def last_update(self):
"""Return the last_update time for the device."""
return parse_datetime(self.device.last_update)

View File

@ -17,8 +17,8 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util.dt import parse_datetime
from . import MinutPointEntity
from .const import DOMAIN as POINT_DOMAIN, POINT_DISCOVERY_NEW
from .entity import MinutPointEntity
_LOGGER = logging.getLogger(__name__)