forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add binary sensor to Netatmo (home-assistant#115119)
* Add binary sensor to Netatmo * Update homeassistant/components/netatmo/binary_sensor.py Co-authored-by: Jan-Philipp Benecke <github@bnck.me> * Sigh * Fix * Fix * Fix --------- Co-authored-by: Jan-Philipp Benecke <github@bnck.me>
- Loading branch information
Showing
6 changed files
with
680 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
"""Support for Netatmo binary sensors.""" | ||
|
||
from homeassistant.components.binary_sensor import ( | ||
BinarySensorDeviceClass, | ||
BinarySensorEntity, | ||
BinarySensorEntityDescription, | ||
) | ||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.core import HomeAssistant, callback | ||
from homeassistant.helpers.dispatcher import async_dispatcher_connect | ||
from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
|
||
from .const import NETATMO_CREATE_WEATHER_SENSOR | ||
from .data_handler import NetatmoDevice | ||
from .entity import NetatmoWeatherModuleEntity | ||
|
||
BINARY_SENSOR_TYPES: tuple[BinarySensorEntityDescription, ...] = ( | ||
BinarySensorEntityDescription( | ||
key="reachable", | ||
device_class=BinarySensorDeviceClass.CONNECTIVITY, | ||
), | ||
) | ||
|
||
|
||
async def async_setup_entry( | ||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback | ||
) -> None: | ||
"""Set up Netatmo binary sensors based on a config entry.""" | ||
|
||
@callback | ||
def _create_weather_binary_sensor_entity(netatmo_device: NetatmoDevice) -> None: | ||
async_add_entities( | ||
NetatmoWeatherBinarySensor(netatmo_device, description) | ||
for description in BINARY_SENSOR_TYPES | ||
if description.key in netatmo_device.device.features | ||
) | ||
|
||
entry.async_on_unload( | ||
async_dispatcher_connect( | ||
hass, NETATMO_CREATE_WEATHER_SENSOR, _create_weather_binary_sensor_entity | ||
) | ||
) | ||
|
||
|
||
class NetatmoWeatherBinarySensor(NetatmoWeatherModuleEntity, BinarySensorEntity): | ||
"""Implementation of a Netatmo binary sensor.""" | ||
|
||
def __init__( | ||
self, device: NetatmoDevice, description: BinarySensorEntityDescription | ||
) -> None: | ||
"""Initialize a Netatmo binary sensor.""" | ||
super().__init__(device) | ||
self.entity_description = description | ||
self._attr_unique_id = f"{self.device.entity_id}-{description.key}" | ||
|
||
@callback | ||
def async_update_callback(self) -> None: | ||
"""Update the entity's state.""" | ||
self._attr_is_on = self.device.reachable | ||
self.async_write_ha_state() |
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
Oops, something went wrong.