mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Add sensor to switchbot platform (#56416)
Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
parent
6e7bc65e2e
commit
972db29c88
@ -1019,6 +1019,7 @@ omit =
|
|||||||
homeassistant/components/switchbot/const.py
|
homeassistant/components/switchbot/const.py
|
||||||
homeassistant/components/switchbot/entity.py
|
homeassistant/components/switchbot/entity.py
|
||||||
homeassistant/components/switchbot/cover.py
|
homeassistant/components/switchbot/cover.py
|
||||||
|
homeassistant/components/switchbot/sensor.py
|
||||||
homeassistant/components/switchbot/coordinator.py
|
homeassistant/components/switchbot/coordinator.py
|
||||||
homeassistant/components/switchmate/switch.py
|
homeassistant/components/switchmate/switch.py
|
||||||
homeassistant/components/syncthing/__init__.py
|
homeassistant/components/syncthing/__init__.py
|
||||||
|
@ -27,8 +27,8 @@ from .const import (
|
|||||||
from .coordinator import SwitchbotDataUpdateCoordinator
|
from .coordinator import SwitchbotDataUpdateCoordinator
|
||||||
|
|
||||||
PLATFORMS_BY_TYPE = {
|
PLATFORMS_BY_TYPE = {
|
||||||
ATTR_BOT: ["switch"],
|
ATTR_BOT: ["switch", "sensor"],
|
||||||
ATTR_CURTAIN: ["cover"],
|
ATTR_CURTAIN: ["cover", "sensor"],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@ class SwitchbotEntity(CoordinatorEntity, Entity):
|
|||||||
self._attr_name = name
|
self._attr_name = name
|
||||||
self._attr_device_info: DeviceInfo = {
|
self._attr_device_info: DeviceInfo = {
|
||||||
"connections": {(dr.CONNECTION_NETWORK_MAC, self._mac)},
|
"connections": {(dr.CONNECTION_NETWORK_MAC, self._mac)},
|
||||||
"name": self._attr_name,
|
"name": name,
|
||||||
"model": self.data["modelName"],
|
"model": self.data["modelName"],
|
||||||
"manufacturer": MANUFACTURER,
|
"manufacturer": MANUFACTURER,
|
||||||
}
|
}
|
||||||
|
93
homeassistant/components/switchbot/sensor.py
Normal file
93
homeassistant/components/switchbot/sensor.py
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
"""Support for SwitchBot sensors."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
||||||
|
from homeassistant.config_entries import ConfigEntry
|
||||||
|
from homeassistant.const import (
|
||||||
|
CONF_MAC,
|
||||||
|
CONF_NAME,
|
||||||
|
DEVICE_CLASS_BATTERY,
|
||||||
|
DEVICE_CLASS_ILLUMINANCE,
|
||||||
|
DEVICE_CLASS_SIGNAL_STRENGTH,
|
||||||
|
PERCENTAGE,
|
||||||
|
SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
|
||||||
|
)
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
|
from .const import DATA_COORDINATOR, DOMAIN
|
||||||
|
from .coordinator import SwitchbotDataUpdateCoordinator
|
||||||
|
from .entity import SwitchbotEntity
|
||||||
|
|
||||||
|
PARALLEL_UPDATES = 1
|
||||||
|
|
||||||
|
SENSOR_TYPES: dict[str, SensorEntityDescription] = {
|
||||||
|
"rssi": SensorEntityDescription(
|
||||||
|
key="rssi",
|
||||||
|
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
|
||||||
|
device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
|
||||||
|
entity_registry_enabled_default=False,
|
||||||
|
),
|
||||||
|
"battery": SensorEntityDescription(
|
||||||
|
key="battery",
|
||||||
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
device_class=DEVICE_CLASS_BATTERY,
|
||||||
|
),
|
||||||
|
"lightLevel": SensorEntityDescription(
|
||||||
|
key="lightLevel",
|
||||||
|
native_unit_of_measurement="Level",
|
||||||
|
device_class=DEVICE_CLASS_ILLUMINANCE,
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
|
) -> None:
|
||||||
|
"""Set up Switchbot sensor based on a config entry."""
|
||||||
|
coordinator: SwitchbotDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][
|
||||||
|
DATA_COORDINATOR
|
||||||
|
]
|
||||||
|
|
||||||
|
if not coordinator.data[entry.unique_id].get("data"):
|
||||||
|
return
|
||||||
|
|
||||||
|
async_add_entities(
|
||||||
|
[
|
||||||
|
SwitchBotSensor(
|
||||||
|
coordinator,
|
||||||
|
entry.unique_id,
|
||||||
|
sensor,
|
||||||
|
entry.data[CONF_MAC],
|
||||||
|
entry.data[CONF_NAME],
|
||||||
|
)
|
||||||
|
for sensor in coordinator.data[entry.unique_id]["data"]
|
||||||
|
if sensor in SENSOR_TYPES
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class SwitchBotSensor(SwitchbotEntity, SensorEntity):
|
||||||
|
"""Representation of a Switchbot sensor."""
|
||||||
|
|
||||||
|
coordinator: SwitchbotDataUpdateCoordinator
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
coordinator: SwitchbotDataUpdateCoordinator,
|
||||||
|
idx: str | None,
|
||||||
|
sensor: str,
|
||||||
|
mac: str,
|
||||||
|
switchbot_name: str,
|
||||||
|
) -> None:
|
||||||
|
"""Initialize the Switchbot sensor."""
|
||||||
|
super().__init__(coordinator, idx, mac, name=switchbot_name)
|
||||||
|
self._sensor = sensor
|
||||||
|
self._attr_unique_id = f"{idx}-{sensor}"
|
||||||
|
self._attr_name = f"{switchbot_name} {sensor.title()}"
|
||||||
|
self.entity_description = SENSOR_TYPES[sensor]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def native_value(self) -> str:
|
||||||
|
"""Return the state of the sensor."""
|
||||||
|
return self.data["data"][self._sensor]
|
@ -111,7 +111,7 @@ class SwitchBotBotEntity(SwitchbotEntity, SwitchEntity, RestoreEntity):
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the Switchbot."""
|
"""Initialize the Switchbot."""
|
||||||
super().__init__(coordinator, idx, mac, name)
|
super().__init__(coordinator, idx, mac, name)
|
||||||
self._attr_unique_id = self._mac.replace(":", "")
|
self._attr_unique_id = idx
|
||||||
self._device = device
|
self._device = device
|
||||||
|
|
||||||
async def async_added_to_hass(self) -> None:
|
async def async_added_to_hass(self) -> None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user