-
-
Notifications
You must be signed in to change notification settings - Fork 28.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement support for SwitchBot Meter, MeterPlus, and Outdoor Meter (#…
…115522) * Implement support for SwitchBot MeterPlus Add temperature, humidity, and battery sensor entities for the MeterPlus device * Rename GH username * Update homeassistant/components/switchbot_cloud/coordinator.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Refactor to use EntityDescriptions Concat entity ID in SwitchBotCloudSensor init * Remove __future__ import * Make scan interval user configurable * Add support for Meter and Outdoor Meter * Revert "Make scan interval user configurable" This reverts commit e256c35. * Remove device-specific default scan intervals * Update homeassistant/components/switchbot_cloud/sensor.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/switchbot_cloud/sensor.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/switchbot_cloud/sensor.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Fix ruff errors * Reorder manifest keys * Update CODEOWNERS * Add sensor.py to coveragerc --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
- Loading branch information
1 parent
548eb35
commit c3f95a4
Showing
7 changed files
with
106 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
"""Platform for sensor integration.""" | ||
|
||
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 PERCENTAGE, UnitOfTemperature | ||
from homeassistant.core import HomeAssistant, callback | ||
from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
|
||
from . import SwitchbotCloudData | ||
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, | ||
device_class=SensorDeviceClass.TEMPERATURE, | ||
state_class=SensorStateClass.MEASUREMENT, | ||
native_unit_of_measurement=UnitOfTemperature.CELSIUS, | ||
), | ||
SensorEntityDescription( | ||
key=SENSOR_TYPE_HUMIDITY, | ||
device_class=SensorDeviceClass.HUMIDITY, | ||
state_class=SensorStateClass.MEASUREMENT, | ||
native_unit_of_measurement=PERCENTAGE, | ||
), | ||
SensorEntityDescription( | ||
key=SENSOR_TYPE_BATTERY, | ||
device_class=SensorDeviceClass.BATTERY, | ||
state_class=SensorStateClass.MEASUREMENT, | ||
native_unit_of_measurement=PERCENTAGE, | ||
), | ||
) | ||
|
||
|
||
async def async_setup_entry( | ||
hass: HomeAssistant, | ||
config: ConfigEntry, | ||
async_add_entities: AddEntitiesCallback, | ||
) -> None: | ||
"""Set up SwitchBot Cloud entry.""" | ||
data: SwitchbotCloudData = hass.data[DOMAIN][config.entry_id] | ||
|
||
async_add_entities( | ||
SwitchBotCloudSensor(data.api, device, coordinator, description) | ||
for device, coordinator in data.devices.sensors | ||
for description in METER_PLUS_SENSOR_DESCRIPTIONS | ||
) | ||
|
||
|
||
class SwitchBotCloudSensor(SwitchBotCloudEntity, SensorEntity): | ||
"""Representation of a SwitchBot Cloud sensor entity.""" | ||
|
||
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 not self.coordinator.data: | ||
return | ||
self._attr_native_value = self.coordinator.data.get(self.entity_description.key) | ||
self.async_write_ha_state() |