mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 00:07:10 +00:00
Add sensor platform to microBees (#111008)
* add microBees sensor * add a sensor.py in .coveragerc * fixes review --------- Co-authored-by: FedDam <noceracity@gmail.com>
This commit is contained in:
parent
ad94534d37
commit
cd05972276
@ -775,6 +775,7 @@ omit =
|
|||||||
homeassistant/components/microbees/const.py
|
homeassistant/components/microbees/const.py
|
||||||
homeassistant/components/microbees/coordinator.py
|
homeassistant/components/microbees/coordinator.py
|
||||||
homeassistant/components/microbees/entity.py
|
homeassistant/components/microbees/entity.py
|
||||||
|
homeassistant/components/microbees/sensor.py
|
||||||
homeassistant/components/microbees/switch.py
|
homeassistant/components/microbees/switch.py
|
||||||
homeassistant/components/microsoft/tts.py
|
homeassistant/components/microsoft/tts.py
|
||||||
homeassistant/components/mikrotik/hub.py
|
homeassistant/components/mikrotik/hub.py
|
||||||
|
@ -5,5 +5,6 @@ DOMAIN = "microbees"
|
|||||||
OAUTH2_AUTHORIZE = "https://dev.microbees.com/oauth/authorize"
|
OAUTH2_AUTHORIZE = "https://dev.microbees.com/oauth/authorize"
|
||||||
OAUTH2_TOKEN = "https://dev.microbees.com/oauth/token"
|
OAUTH2_TOKEN = "https://dev.microbees.com/oauth/token"
|
||||||
PLATFORMS = [
|
PLATFORMS = [
|
||||||
|
Platform.SENSOR,
|
||||||
Platform.SWITCH,
|
Platform.SWITCH,
|
||||||
]
|
]
|
||||||
|
@ -7,7 +7,7 @@ from http import HTTPStatus
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
from microBeesPy import Actuator, Bee, MicroBees, MicroBeesException
|
from microBeesPy import Actuator, Bee, MicroBees, MicroBeesException, Sensor
|
||||||
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryAuthFailed
|
from homeassistant.exceptions import ConfigEntryAuthFailed
|
||||||
@ -22,6 +22,7 @@ class MicroBeesCoordinatorData:
|
|||||||
|
|
||||||
bees: dict[int, Bee]
|
bees: dict[int, Bee]
|
||||||
actuators: dict[int, Actuator]
|
actuators: dict[int, Actuator]
|
||||||
|
sensors: dict[int, Sensor]
|
||||||
|
|
||||||
|
|
||||||
class MicroBeesUpdateCoordinator(DataUpdateCoordinator[MicroBeesCoordinatorData]):
|
class MicroBeesUpdateCoordinator(DataUpdateCoordinator[MicroBeesCoordinatorData]):
|
||||||
@ -54,8 +55,13 @@ class MicroBeesUpdateCoordinator(DataUpdateCoordinator[MicroBeesCoordinatorData]
|
|||||||
|
|
||||||
bees_dict = {}
|
bees_dict = {}
|
||||||
actuators_dict = {}
|
actuators_dict = {}
|
||||||
|
sensors_dict = {}
|
||||||
for bee in bees:
|
for bee in bees:
|
||||||
bees_dict[bee.id] = bee
|
bees_dict[bee.id] = bee
|
||||||
for actuator in bee.actuators:
|
for actuator in bee.actuators:
|
||||||
actuators_dict[actuator.id] = actuator
|
actuators_dict[actuator.id] = actuator
|
||||||
return MicroBeesCoordinatorData(bees=bees_dict, actuators=actuators_dict)
|
for sensor in bee.sensors:
|
||||||
|
sensors_dict[sensor.id] = sensor
|
||||||
|
return MicroBeesCoordinatorData(
|
||||||
|
bees=bees_dict, actuators=actuators_dict, sensors=sensors_dict
|
||||||
|
)
|
||||||
|
109
homeassistant/components/microbees/sensor.py
Normal file
109
homeassistant/components/microbees/sensor.py
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
"""sensor integration microBees."""
|
||||||
|
from microBeesPy import Sensor
|
||||||
|
|
||||||
|
from homeassistant.components.sensor import (
|
||||||
|
SensorDeviceClass,
|
||||||
|
SensorEntity,
|
||||||
|
SensorEntityDescription,
|
||||||
|
SensorStateClass,
|
||||||
|
)
|
||||||
|
from homeassistant.config_entries import ConfigEntry
|
||||||
|
from homeassistant.const import (
|
||||||
|
CONCENTRATION_PARTS_PER_MILLION,
|
||||||
|
LIGHT_LUX,
|
||||||
|
PERCENTAGE,
|
||||||
|
UnitOfPower,
|
||||||
|
UnitOfTemperature,
|
||||||
|
)
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
|
from .const import DOMAIN
|
||||||
|
from .coordinator import MicroBeesUpdateCoordinator
|
||||||
|
from .entity import MicroBeesEntity
|
||||||
|
|
||||||
|
SENSOR_TYPES = {
|
||||||
|
0: SensorEntityDescription(
|
||||||
|
device_class=SensorDeviceClass.POWER,
|
||||||
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
native_unit_of_measurement=UnitOfPower.WATT,
|
||||||
|
key="absorption",
|
||||||
|
suggested_display_precision=2,
|
||||||
|
),
|
||||||
|
2: SensorEntityDescription(
|
||||||
|
device_class=SensorDeviceClass.TEMPERATURE,
|
||||||
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
||||||
|
key="temperature",
|
||||||
|
suggested_display_precision=1,
|
||||||
|
),
|
||||||
|
14: SensorEntityDescription(
|
||||||
|
device_class=SensorDeviceClass.CO2,
|
||||||
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
|
||||||
|
key="carbon_dioxide",
|
||||||
|
suggested_display_precision=1,
|
||||||
|
),
|
||||||
|
16: SensorEntityDescription(
|
||||||
|
device_class=SensorDeviceClass.HUMIDITY,
|
||||||
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
key="humidity",
|
||||||
|
suggested_display_precision=1,
|
||||||
|
),
|
||||||
|
21: SensorEntityDescription(
|
||||||
|
device_class=SensorDeviceClass.ILLUMINANCE,
|
||||||
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
native_unit_of_measurement=LIGHT_LUX,
|
||||||
|
key="illuminance",
|
||||||
|
suggested_display_precision=1,
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
|
) -> None:
|
||||||
|
"""Config entry."""
|
||||||
|
coordinator = hass.data[DOMAIN][entry.entry_id].coordinator
|
||||||
|
sensors = []
|
||||||
|
for bee_id, bee in coordinator.data.bees.items():
|
||||||
|
for sensor in bee.sensors:
|
||||||
|
if (entity_description := SENSOR_TYPES.get(sensor.device_type)) is not None:
|
||||||
|
sensors.append(
|
||||||
|
MBSensor(coordinator, entity_description, bee_id, sensor.id)
|
||||||
|
)
|
||||||
|
|
||||||
|
async_add_entities(sensors)
|
||||||
|
|
||||||
|
|
||||||
|
class MBSensor(MicroBeesEntity, SensorEntity):
|
||||||
|
"""Representation of a microBees sensor."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
coordinator: MicroBeesUpdateCoordinator,
|
||||||
|
entity_description: SensorEntityDescription,
|
||||||
|
bee_id: int,
|
||||||
|
sensor_id: int,
|
||||||
|
) -> None:
|
||||||
|
"""Initialize the microBees sensor."""
|
||||||
|
super().__init__(coordinator, bee_id)
|
||||||
|
self._attr_unique_id = f"{bee_id}_{sensor_id}"
|
||||||
|
self.sensor_id = sensor_id
|
||||||
|
self.entity_description = entity_description
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self) -> str:
|
||||||
|
"""Name of the sensor."""
|
||||||
|
return self.sensor.name
|
||||||
|
|
||||||
|
@property
|
||||||
|
def native_value(self) -> float | None:
|
||||||
|
"""Return the value reported by the sensor, or None if the relevant sensor can't produce a current measurement."""
|
||||||
|
return self.sensor.value
|
||||||
|
|
||||||
|
@property
|
||||||
|
def sensor(self) -> Sensor:
|
||||||
|
"""Return the sensor."""
|
||||||
|
return self.coordinator.data.sensors[self.sensor_id]
|
Loading…
x
Reference in New Issue
Block a user