mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Use EntityDescription - ios (#54359)
* Use EntityDescription - ios * Make attribute static * Update homeassistant/components/ios/sensor.py Co-authored-by: Franck Nijhof <frenck@frenck.nl> Co-authored-by: Franck Nijhof <frenck@frenck.nl>
This commit is contained in:
parent
7e211965e4
commit
f5901265dc
@ -1,6 +1,8 @@
|
|||||||
"""Support for Home Assistant iOS app sensors."""
|
"""Support for Home Assistant iOS app sensors."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
from homeassistant.components import ios
|
from homeassistant.components import ios
|
||||||
from homeassistant.components.sensor import SensorEntity
|
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
||||||
from homeassistant.const import PERCENTAGE
|
from homeassistant.const import PERCENTAGE
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
@ -8,10 +10,17 @@ from homeassistant.helpers.icon import icon_for_battery_level
|
|||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
|
||||||
SENSOR_TYPES = {
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||||
"level": ["Battery Level", PERCENTAGE],
|
SensorEntityDescription(
|
||||||
"state": ["Battery State", None],
|
key="level",
|
||||||
}
|
name="Battery Level",
|
||||||
|
unit_of_measurement=PERCENTAGE,
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key="state",
|
||||||
|
name="Battery State",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
DEFAULT_ICON_LEVEL = "mdi:battery"
|
DEFAULT_ICON_LEVEL = "mdi:battery"
|
||||||
DEFAULT_ICON_STATE = "mdi:power-plug"
|
DEFAULT_ICON_STATE = "mdi:power-plug"
|
||||||
@ -24,25 +33,30 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
|
|
||||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
"""Set up iOS from a config entry."""
|
"""Set up iOS from a config entry."""
|
||||||
dev = []
|
entities = [
|
||||||
for device_name, device in ios.devices(hass).items():
|
IOSSensor(device_name, device, description)
|
||||||
for sensor_type in ("level", "state"):
|
for device_name, device in ios.devices(hass).items()
|
||||||
dev.append(IOSSensor(sensor_type, device_name, device))
|
for description in SENSOR_TYPES
|
||||||
|
]
|
||||||
|
|
||||||
async_add_entities(dev, True)
|
async_add_entities(entities, True)
|
||||||
|
|
||||||
|
|
||||||
class IOSSensor(SensorEntity):
|
class IOSSensor(SensorEntity):
|
||||||
"""Representation of an iOS sensor."""
|
"""Representation of an iOS sensor."""
|
||||||
|
|
||||||
def __init__(self, sensor_type, device_name, device):
|
_attr_should_poll = False
|
||||||
|
|
||||||
|
def __init__(self, device_name, device, description: SensorEntityDescription):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self._device_name = device_name
|
self.entity_description = description
|
||||||
self._name = f"{device_name} {SENSOR_TYPES[sensor_type][0]}"
|
|
||||||
self._device = device
|
self._device = device
|
||||||
self.type = sensor_type
|
|
||||||
self._state = None
|
device_name = device[ios.ATTR_DEVICE][ios.ATTR_DEVICE_NAME]
|
||||||
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
|
self._attr_name = f"{device_name} {description.key}"
|
||||||
|
|
||||||
|
device_id = device[ios.ATTR_DEVICE_ID]
|
||||||
|
self._attr_unique_id = f"{description.key}_{device_id}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_info(self):
|
def device_info(self):
|
||||||
@ -60,33 +74,6 @@ class IOSSensor(SensorEntity):
|
|||||||
"sw_version": self._device[ios.ATTR_DEVICE][ios.ATTR_DEVICE_SYSTEM_VERSION],
|
"sw_version": self._device[ios.ATTR_DEVICE][ios.ATTR_DEVICE_SYSTEM_VERSION],
|
||||||
}
|
}
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self):
|
|
||||||
"""Return the name of the iOS sensor."""
|
|
||||||
device_name = self._device[ios.ATTR_DEVICE][ios.ATTR_DEVICE_NAME]
|
|
||||||
return f"{device_name} {SENSOR_TYPES[self.type][0]}"
|
|
||||||
|
|
||||||
@property
|
|
||||||
def state(self):
|
|
||||||
"""Return the state of the sensor."""
|
|
||||||
return self._state
|
|
||||||
|
|
||||||
@property
|
|
||||||
def unique_id(self):
|
|
||||||
"""Return the unique ID of this sensor."""
|
|
||||||
device_id = self._device[ios.ATTR_DEVICE_ID]
|
|
||||||
return f"{self.type}_{device_id}"
|
|
||||||
|
|
||||||
@property
|
|
||||||
def should_poll(self):
|
|
||||||
"""No polling needed."""
|
|
||||||
return False
|
|
||||||
|
|
||||||
@property
|
|
||||||
def unit_of_measurement(self):
|
|
||||||
"""Return the unit of measurement this sensor expresses itself in."""
|
|
||||||
return self._unit_of_measurement
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def extra_state_attributes(self):
|
def extra_state_attributes(self):
|
||||||
"""Return the device state attributes."""
|
"""Return the device state attributes."""
|
||||||
@ -119,7 +106,7 @@ class IOSSensor(SensorEntity):
|
|||||||
charging = False
|
charging = False
|
||||||
icon_state = f"{DEFAULT_ICON_LEVEL}-unknown"
|
icon_state = f"{DEFAULT_ICON_LEVEL}-unknown"
|
||||||
|
|
||||||
if self.type == "state":
|
if self.entity_description.key == "state":
|
||||||
return icon_state
|
return icon_state
|
||||||
return icon_for_battery_level(battery_level=battery_level, charging=charging)
|
return icon_for_battery_level(battery_level=battery_level, charging=charging)
|
||||||
|
|
||||||
@ -127,12 +114,12 @@ class IOSSensor(SensorEntity):
|
|||||||
def _update(self, device):
|
def _update(self, device):
|
||||||
"""Get the latest state of the sensor."""
|
"""Get the latest state of the sensor."""
|
||||||
self._device = device
|
self._device = device
|
||||||
self._state = self._device[ios.ATTR_BATTERY][self.type]
|
self._attr_state = self._device[ios.ATTR_BATTERY][self.entity_description.key]
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
async def async_added_to_hass(self) -> None:
|
async def async_added_to_hass(self) -> None:
|
||||||
"""Added to hass so need to register to dispatch."""
|
"""Added to hass so need to register to dispatch."""
|
||||||
self._state = self._device[ios.ATTR_BATTERY][self.type]
|
self._attr_state = self._device[ios.ATTR_BATTERY][self.entity_description.key]
|
||||||
device_id = self._device[ios.ATTR_DEVICE_ID]
|
device_id = self._device[ios.ATTR_DEVICE_ID]
|
||||||
self.async_on_remove(
|
self.async_on_remove(
|
||||||
async_dispatcher_connect(self.hass, f"{DOMAIN}.{device_id}", self._update)
|
async_dispatcher_connect(self.hass, f"{DOMAIN}.{device_id}", self._update)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user