Add Mysensors battery sensor (#100749)
* Move child related stuff to MySensorsChildEntity * Dispatch signal for newly discovered MySensors node * Create battery entity for each MySensors node * Removed ATTR_BATTERY_LEVEL attribute from each node sensor Attribute is redundant with newly introduced battery sensor entity * Apply suggestions from code review --------- Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
6d624ecb46
commit
09729e8c46
18 changed files with 303 additions and 139 deletions
|
@ -4,6 +4,7 @@ from __future__ import annotations
|
|||
from typing import Any
|
||||
|
||||
from awesomeversion import AwesomeVersion
|
||||
from mysensors import BaseAsyncGateway
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
SensorDeviceClass,
|
||||
|
@ -30,13 +31,22 @@ from homeassistant.const import (
|
|||
UnitOfTemperature,
|
||||
UnitOfVolume,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.util.unit_system import METRIC_SYSTEM
|
||||
|
||||
from .. import mysensors
|
||||
from .const import MYSENSORS_DISCOVERY, DiscoveryInfo
|
||||
from .const import (
|
||||
ATTR_GATEWAY_ID,
|
||||
ATTR_NODE_ID,
|
||||
DOMAIN,
|
||||
MYSENSORS_DISCOVERY,
|
||||
MYSENSORS_GATEWAYS,
|
||||
MYSENSORS_NODE_DISCOVERY,
|
||||
DiscoveryInfo,
|
||||
NodeDiscoveryInfo,
|
||||
)
|
||||
from .helpers import on_unload
|
||||
|
||||
SENSORS: dict[str, SensorEntityDescription] = {
|
||||
|
@ -211,6 +221,14 @@ async def async_setup_entry(
|
|||
async_add_entities=async_add_entities,
|
||||
)
|
||||
|
||||
@callback
|
||||
def async_node_discover(discovery_info: NodeDiscoveryInfo) -> None:
|
||||
"""Add battery sensor for each MySensors node."""
|
||||
gateway_id = discovery_info[ATTR_GATEWAY_ID]
|
||||
node_id = discovery_info[ATTR_NODE_ID]
|
||||
gateway: BaseAsyncGateway = hass.data[DOMAIN][MYSENSORS_GATEWAYS][gateway_id]
|
||||
async_add_entities([MyBatterySensor(gateway_id, gateway, node_id)])
|
||||
|
||||
on_unload(
|
||||
hass,
|
||||
config_entry.entry_id,
|
||||
|
@ -221,8 +239,43 @@ async def async_setup_entry(
|
|||
),
|
||||
)
|
||||
|
||||
on_unload(
|
||||
hass,
|
||||
config_entry.entry_id,
|
||||
async_dispatcher_connect(
|
||||
hass,
|
||||
MYSENSORS_NODE_DISCOVERY,
|
||||
async_node_discover,
|
||||
),
|
||||
)
|
||||
|
||||
class MySensorsSensor(mysensors.device.MySensorsEntity, SensorEntity):
|
||||
|
||||
class MyBatterySensor(mysensors.device.MySensorNodeEntity, SensorEntity):
|
||||
"""Battery sensor of MySensors node."""
|
||||
|
||||
_attr_device_class = SensorDeviceClass.BATTERY
|
||||
_attr_state_class = SensorStateClass.MEASUREMENT
|
||||
_attr_native_unit_of_measurement = PERCENTAGE
|
||||
_attr_force_update = True
|
||||
|
||||
@property
|
||||
def unique_id(self) -> str:
|
||||
"""Return a unique ID for use in home assistant."""
|
||||
return f"{self.gateway_id}-{self.node_id}-battery"
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""Return the name of this entity."""
|
||||
return f"{self.node_name} Battery"
|
||||
|
||||
@callback
|
||||
def _async_update_callback(self) -> None:
|
||||
"""Update the controller with the latest battery level."""
|
||||
self._attr_native_value = self._node.battery_level
|
||||
self.async_write_ha_state()
|
||||
|
||||
|
||||
class MySensorsSensor(mysensors.device.MySensorsChildEntity, SensorEntity):
|
||||
"""Representation of a MySensors Sensor child node."""
|
||||
|
||||
_attr_force_update = True
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue