* Update switchbot to be local push * fixes * fixes * fixes * fixes * adjust * cover is not assumed anymore * cleanups * adjust * adjust * add missing cover * import compat * fixes * uses lower * uses lower * bleak users upper case addresses * fixes * bump * keep conf_mac and deprecated options for rollback * reuse coordinator * adjust * move around * move around * move around * move around * refactor fixes * compat with DataUpdateCoordinator * fix available * Update homeassistant/components/bluetooth/passive_update_processor.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/bluetooth/passive_update_coordinator.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/bluetooth/update_coordinator.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Split bluetooth coordinator into PassiveBluetoothDataUpdateCoordinator and PassiveBluetoothProcessorCoordinator The PassiveBluetoothDataUpdateCoordinator is now used to replace instances of DataUpdateCoordinator where the data is coming from bluetooth advertisements, and the integration may also mix in active updates The PassiveBluetoothProcessorCoordinator is used for integrations that want to process each bluetooth advertisement with multiple processors which can be dispatched to individual platforms or areas or the integration as it chooes * change connections * reduce code churn to reduce review overhead * reduce code churn to reduce review overhead * Update homeassistant/components/bluetooth/passive_update_coordinator.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * add basic test * add basic test * complete coverage * Update homeassistant/components/switchbot/coordinator.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/switchbot/coordinator.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/switchbot/__init__.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/switchbot/__init__.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * lint Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
102 lines
3.2 KiB
Python
102 lines
3.2 KiB
Python
"""Support for SwitchBot sensors."""
|
|
from __future__ import annotations
|
|
|
|
from homeassistant.components.sensor import (
|
|
SensorDeviceClass,
|
|
SensorEntity,
|
|
SensorEntityDescription,
|
|
)
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.const import (
|
|
CONF_ADDRESS,
|
|
CONF_NAME,
|
|
PERCENTAGE,
|
|
SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
|
|
TEMP_CELSIUS,
|
|
)
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers.entity import EntityCategory
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from .const import 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=SensorDeviceClass.SIGNAL_STRENGTH,
|
|
entity_registry_enabled_default=False,
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
),
|
|
"battery": SensorEntityDescription(
|
|
key="battery",
|
|
native_unit_of_measurement=PERCENTAGE,
|
|
device_class=SensorDeviceClass.BATTERY,
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
),
|
|
"lightLevel": SensorEntityDescription(
|
|
key="lightLevel",
|
|
native_unit_of_measurement="Level",
|
|
device_class=SensorDeviceClass.ILLUMINANCE,
|
|
),
|
|
"humidity": SensorEntityDescription(
|
|
key="humidity",
|
|
native_unit_of_measurement=PERCENTAGE,
|
|
device_class=SensorDeviceClass.HUMIDITY,
|
|
),
|
|
"temperature": SensorEntityDescription(
|
|
key="temperature",
|
|
native_unit_of_measurement=TEMP_CELSIUS,
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
|
),
|
|
}
|
|
|
|
|
|
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]
|
|
unique_id = entry.unique_id
|
|
assert unique_id is not None
|
|
async_add_entities(
|
|
[
|
|
SwitchBotSensor(
|
|
coordinator,
|
|
unique_id,
|
|
sensor,
|
|
entry.data[CONF_ADDRESS],
|
|
entry.data[CONF_NAME],
|
|
)
|
|
for sensor in coordinator.data["data"]
|
|
if sensor in SENSOR_TYPES
|
|
]
|
|
)
|
|
|
|
|
|
class SwitchBotSensor(SwitchbotEntity, SensorEntity):
|
|
"""Representation of a Switchbot sensor."""
|
|
|
|
def __init__(
|
|
self,
|
|
coordinator: SwitchbotDataUpdateCoordinator,
|
|
unique_id: str,
|
|
sensor: str,
|
|
address: str,
|
|
switchbot_name: str,
|
|
) -> None:
|
|
"""Initialize the Switchbot sensor."""
|
|
super().__init__(coordinator, unique_id, address, name=switchbot_name)
|
|
self._sensor = sensor
|
|
self._attr_unique_id = f"{unique_id}-{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]
|