-
-
Notifications
You must be signed in to change notification settings - Fork 38.2k
Add yolink binary sensor #72000
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MartinHjelmare
merged 3 commits into
home-assistant:dev
from
YoSmart-Inc:yolink-feature-binarysensor
May 19, 2022
Merged
Add yolink binary sensor #72000
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,98 @@ | ||
| """YoLink BinarySensor.""" | ||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Callable | ||
| from dataclasses import dataclass | ||
|
|
||
| from yolink.device import YoLinkDevice | ||
|
|
||
| from homeassistant.components.binary_sensor import ( | ||
| BinarySensorDeviceClass, | ||
| BinarySensorEntity, | ||
| BinarySensorEntityDescription, | ||
| ) | ||
| from homeassistant.config_entries import ConfigEntry | ||
| from homeassistant.core import HomeAssistant, callback | ||
| from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
|
|
||
| from .const import ATTR_COORDINATOR, ATTR_DEVICE_DOOR_SENSOR, DOMAIN | ||
| from .coordinator import YoLinkCoordinator | ||
| from .entity import YoLinkEntity | ||
|
|
||
|
|
||
| @dataclass | ||
| class YoLinkBinarySensorEntityDescriptionMixin: | ||
| """Mixin for device type.""" | ||
|
|
||
| exists_fn: Callable[[YoLinkDevice], bool] = lambda _: True | ||
|
|
||
|
|
||
| @dataclass | ||
| class YoLinkBinarySensorEntityDescription( | ||
| YoLinkBinarySensorEntityDescriptionMixin, BinarySensorEntityDescription | ||
| ): | ||
| """YoLink BinarySensorEntityDescription.""" | ||
|
|
||
| value: Callable = lambda state: state | ||
|
MartinHjelmare marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| SENSOR_DEVICE_TYPE = [ATTR_DEVICE_DOOR_SENSOR] | ||
|
|
||
| SENSOR_TYPES: tuple[YoLinkBinarySensorEntityDescription, ...] = ( | ||
| YoLinkBinarySensorEntityDescription( | ||
| key="state", | ||
| icon="mdi:door", | ||
| device_class=BinarySensorDeviceClass.DOOR, | ||
| name="state", | ||
| value=lambda value: value == "open", | ||
| exists_fn=lambda device: device.device_type in [ATTR_DEVICE_DOOR_SENSOR], | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| async def async_setup_entry( | ||
| hass: HomeAssistant, | ||
| config_entry: ConfigEntry, | ||
| async_add_entities: AddEntitiesCallback, | ||
| ) -> None: | ||
| """Set up YoLink Sensor from a config entry.""" | ||
| coordinator = hass.data[DOMAIN][config_entry.entry_id][ATTR_COORDINATOR] | ||
| sensor_devices = [ | ||
| device | ||
| for device in coordinator.yl_devices | ||
| if device.device_type in SENSOR_DEVICE_TYPE | ||
| ] | ||
| entities = [] | ||
| for sensor_device in sensor_devices: | ||
| for description in SENSOR_TYPES: | ||
| if description.exists_fn(sensor_device): | ||
| entities.append( | ||
| YoLinkBinarySensorEntity(coordinator, description, sensor_device) | ||
| ) | ||
| async_add_entities(entities) | ||
|
|
||
|
|
||
| class YoLinkBinarySensorEntity(YoLinkEntity, BinarySensorEntity): | ||
| """YoLink Sensor Entity.""" | ||
|
|
||
| entity_description: YoLinkBinarySensorEntityDescription | ||
|
|
||
| def __init__( | ||
| self, | ||
| coordinator: YoLinkCoordinator, | ||
| description: YoLinkBinarySensorEntityDescription, | ||
| device: YoLinkDevice, | ||
| ) -> None: | ||
| """Init YoLink Sensor.""" | ||
| super().__init__(coordinator, device) | ||
| self.entity_description = description | ||
| self._attr_unique_id = f"{device.device_id} {self.entity_description.key}" | ||
| self._attr_name = f"{device.device_name} ({self.entity_description.name})" | ||
|
|
||
| @callback | ||
| def update_entity_state(self, state: dict) -> None: | ||
| """Update HA Entity State.""" | ||
| self._attr_is_on = self.entity_description.value( | ||
| state[self.entity_description.key] | ||
| ) | ||
| self.async_write_ha_state() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.