mirror of
https://github.com/home-assistant/core.git
synced 2025-08-03 12:45:28 +02:00
Refactor to use EntityDescriptions
Concat entity ID in SwitchBotCloudSensor init
This commit is contained in:
@@ -7,3 +7,7 @@ DOMAIN: Final = "switchbot_cloud"
|
||||
ENTRY_TITLE = "SwitchBot Cloud"
|
||||
DEFAULT_SCAN_INTERVAL = timedelta(seconds=600)
|
||||
DEVICE_SCAN_INTERVAL = {"MeterPlus": timedelta(seconds=60)}
|
||||
|
||||
SENSOR_KIND_TEMPERATURE = 'temperature'
|
||||
SENSOR_KIND_HUMIDITY = 'humidity'
|
||||
SENSOR_KIND_BATTERY = 'battery'
|
@@ -23,12 +23,11 @@ class SwitchBotCloudEntity(CoordinatorEntity[SwitchBotCoordinator]):
|
||||
api: SwitchBotAPI,
|
||||
device: Device | Remote,
|
||||
coordinator: SwitchBotCoordinator,
|
||||
id_suffix="", # Add suffix for multi-entity devices
|
||||
) -> None:
|
||||
"""Initialize the entity."""
|
||||
super().__init__(coordinator)
|
||||
self._api = api
|
||||
self._attr_unique_id = f"{device.device_id}{id_suffix}"
|
||||
self._attr_unique_id = device.device_id
|
||||
self._attr_device_info = DeviceInfo(
|
||||
identifiers={(DOMAIN, device.device_id)},
|
||||
name=device.device_name,
|
||||
|
@@ -7,10 +7,11 @@ from switchbot_api import Device, SwitchBotAPI
|
||||
from homeassistant.components.sensor import (
|
||||
SensorDeviceClass,
|
||||
SensorEntity,
|
||||
SensorEntityDescription,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import UnitOfTemperature
|
||||
from homeassistant.const import PERCENTAGE, UnitOfTemperature
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
@@ -20,6 +21,34 @@ from .const import DOMAIN
|
||||
from .coordinator import SwitchBotCoordinator
|
||||
from .entity import SwitchBotCloudEntity
|
||||
|
||||
SENSOR_TYPE_TEMPERATURE = "temperature"
|
||||
SENSOR_TYPE_HUMIDITY = "humidity"
|
||||
SENSOR_TYPE_BATTERY = "battery"
|
||||
|
||||
METER_PLUS_SENSOR_DESCRIPTIONS = (
|
||||
SensorEntityDescription(
|
||||
key=SENSOR_TYPE_TEMPERATURE,
|
||||
name="Temperature",
|
||||
device_class=SensorDeviceClass.TEMPERATURE,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key=SENSOR_TYPE_HUMIDITY,
|
||||
name="Humidity",
|
||||
device_class=SensorDeviceClass.HUMIDITY,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key=SENSOR_TYPE_BATTERY,
|
||||
name="Battery",
|
||||
device_class=SensorDeviceClass.BATTERY,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
@@ -29,91 +58,38 @@ async def async_setup_entry(
|
||||
"""Set up SwitchBot Cloud entry."""
|
||||
data: SwitchbotCloudData = hass.data[DOMAIN][config.entry_id]
|
||||
|
||||
entities_to_add = []
|
||||
|
||||
for device, coordinator in data.devices.sensors:
|
||||
entities_to_add.append(
|
||||
_async_make_entity(
|
||||
data.api, device, coordinator, SensorDeviceClass.TEMPERATURE
|
||||
)
|
||||
)
|
||||
entities_to_add.append(
|
||||
_async_make_entity(
|
||||
data.api, device, coordinator, SensorDeviceClass.HUMIDITY
|
||||
)
|
||||
)
|
||||
entities_to_add.append(
|
||||
_async_make_entity(data.api, device, coordinator, SensorDeviceClass.BATTERY)
|
||||
)
|
||||
|
||||
async_add_entities(entities_to_add)
|
||||
async_add_entities(
|
||||
SwitchBotCloudSensor(data.api, device, coordinator, description)
|
||||
for device, coordinator in data.devices.sensors
|
||||
for description in METER_PLUS_SENSOR_DESCRIPTIONS
|
||||
)
|
||||
|
||||
|
||||
class SwitchBotCloudHumiditySensor(
|
||||
SwitchBotCloudEntity, CoordinatorEntity, SensorEntity
|
||||
class SwitchBotCloudSensor(
|
||||
SwitchBotCloudEntity, CoordinatorEntity[SwitchBotCoordinator], SensorEntity
|
||||
):
|
||||
"""Representation of a Humidity Sensor."""
|
||||
"""Representation of a SwitchBot Cloud sensor entity."""
|
||||
|
||||
_attr_name = "Humidity"
|
||||
_attr_native_unit_of_measurement = "%"
|
||||
_attr_device_class = SensorDeviceClass.HUMIDITY
|
||||
_attr_state_class = SensorStateClass.MEASUREMENT
|
||||
def __init__(
|
||||
self,
|
||||
api: SwitchBotAPI,
|
||||
device: Device,
|
||||
coordinator: SwitchBotCoordinator,
|
||||
description: SensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize SwitchBot Cloud sensor entity."""
|
||||
super().__init__(api, device, coordinator)
|
||||
self.entity_description = description
|
||||
self._attr_unique_id = f"{device.device_id}_{description.key}"
|
||||
|
||||
@callback
|
||||
def _handle_coordinator_update(self) -> None:
|
||||
"""Handle updated data from the coordinator."""
|
||||
if self.coordinator.data:
|
||||
self._attr_native_value = self.coordinator.data.get("humidity")
|
||||
if self.entity_description.key == SENSOR_TYPE_TEMPERATURE:
|
||||
self._attr_native_value = self.coordinator.data.get("temperature")
|
||||
if self.entity_description.key == SENSOR_TYPE_HUMIDITY:
|
||||
self._attr_native_value = self.coordinator.data.get("humidity")
|
||||
if self.entity_description.key == SENSOR_TYPE_BATTERY:
|
||||
self._attr_native_value = self.coordinator.data.get("battery")
|
||||
self.async_write_ha_state()
|
||||
|
||||
|
||||
class SwitchBotCloudTemperatureSensor(
|
||||
SwitchBotCloudEntity, CoordinatorEntity, SensorEntity
|
||||
):
|
||||
"""Representation of a Temperature Sensor."""
|
||||
|
||||
_attr_name = "Temperature"
|
||||
_attr_native_unit_of_measurement = UnitOfTemperature.CELSIUS
|
||||
_attr_device_class = SensorDeviceClass.TEMPERATURE
|
||||
_attr_state_class = SensorStateClass.MEASUREMENT
|
||||
|
||||
@callback
|
||||
def _handle_coordinator_update(self) -> None:
|
||||
"""Handle updated data from the coordinator."""
|
||||
if self.coordinator.data:
|
||||
self._attr_native_value = self.coordinator.data.get("temperature")
|
||||
self.async_write_ha_state()
|
||||
|
||||
|
||||
class SwitchBotCloudBatterySensor(
|
||||
SwitchBotCloudEntity, CoordinatorEntity, SensorEntity
|
||||
):
|
||||
"""Representation of a Battery Sensor."""
|
||||
|
||||
_attr_name = "Battery"
|
||||
_attr_native_unit_of_measurement = "%"
|
||||
_attr_device_class = SensorDeviceClass.BATTERY
|
||||
_attr_state_class = SensorStateClass.MEASUREMENT
|
||||
|
||||
@callback
|
||||
def _handle_coordinator_update(self) -> None:
|
||||
"""Handle updated data from the coordinator."""
|
||||
if self.coordinator.data:
|
||||
self._attr_native_value = self.coordinator.data.get("battery")
|
||||
self.async_write_ha_state()
|
||||
|
||||
|
||||
@callback
|
||||
def _async_make_entity(
|
||||
api: SwitchBotAPI,
|
||||
device: Device,
|
||||
coordinator: SwitchBotCoordinator,
|
||||
device_class: SensorDeviceClass,
|
||||
) -> SensorEntity:
|
||||
"""Make a SwitchBotCloud Sensor."""
|
||||
if device_class == SensorDeviceClass.TEMPERATURE:
|
||||
return SwitchBotCloudTemperatureSensor(api, device, coordinator, "temperature")
|
||||
if device_class == SensorDeviceClass.HUMIDITY:
|
||||
return SwitchBotCloudHumiditySensor(api, device, coordinator, "humidity")
|
||||
if device_class == SensorDeviceClass.BATTERY:
|
||||
return SwitchBotCloudBatterySensor(api, device, coordinator, "battery")
|
||||
|
Reference in New Issue
Block a user