-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Description: **Related issue (if applicable):** fixes #21782 ## Checklist: - [x] The code change is tested and works locally. - [x] Local tests pass with `tox`. **Your PR cannot be merged unless tests pass** - [x] There is no commented out code in this PR.
- Loading branch information
1 parent
6a80ffa
commit dcaced1
Showing
9 changed files
with
529 additions
and
30 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
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,54 @@ | ||
"""Binary sensor platform for mobile_app.""" | ||
from functools import partial | ||
|
||
from homeassistant.const import CONF_WEBHOOK_ID | ||
from homeassistant.core import callback | ||
from homeassistant.components.binary_sensor import BinarySensorDevice | ||
from homeassistant.helpers.dispatcher import async_dispatcher_connect | ||
|
||
from .const import (ATTR_SENSOR_STATE, | ||
ATTR_SENSOR_TYPE_BINARY_SENSOR as ENTITY_TYPE, | ||
DATA_DEVICES, DOMAIN) | ||
|
||
from .entity import MobileAppEntity | ||
|
||
DEPENDENCIES = ['mobile_app'] | ||
|
||
|
||
async def async_setup_entry(hass, config_entry, async_add_entities): | ||
"""Set up mobile app binary sensor from a config entry.""" | ||
entities = list() | ||
|
||
webhook_id = config_entry.data[CONF_WEBHOOK_ID] | ||
|
||
for config in hass.data[DOMAIN][ENTITY_TYPE].values(): | ||
if config[CONF_WEBHOOK_ID] != webhook_id: | ||
continue | ||
|
||
device = hass.data[DOMAIN][DATA_DEVICES][webhook_id] | ||
|
||
entities.append(MobileAppBinarySensor(config, device, config_entry)) | ||
|
||
async_add_entities(entities) | ||
|
||
@callback | ||
def handle_sensor_registration(webhook_id, data): | ||
if data[CONF_WEBHOOK_ID] != webhook_id: | ||
return | ||
|
||
device = hass.data[DOMAIN][DATA_DEVICES][data[CONF_WEBHOOK_ID]] | ||
|
||
async_add_entities([MobileAppBinarySensor(data, device, config_entry)]) | ||
|
||
async_dispatcher_connect(hass, | ||
'{}_{}_register'.format(DOMAIN, ENTITY_TYPE), | ||
partial(handle_sensor_registration, webhook_id)) | ||
|
||
|
||
class MobileAppBinarySensor(MobileAppEntity, BinarySensorDevice): | ||
"""Representation of an mobile app binary sensor.""" | ||
|
||
@property | ||
def is_on(self): | ||
"""Return the state of the binary sensor.""" | ||
return self._config[ATTR_SENSOR_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
"""A entity class for mobile_app.""" | ||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.const import CONF_WEBHOOK_ID | ||
from homeassistant.core import callback | ||
from homeassistant.helpers.device_registry import DeviceEntry | ||
from homeassistant.helpers.dispatcher import async_dispatcher_connect | ||
from homeassistant.helpers.entity import Entity | ||
|
||
from .const import (ATTR_DEVICE_ID, ATTR_DEVICE_NAME, ATTR_MANUFACTURER, | ||
ATTR_MODEL, ATTR_OS_VERSION, ATTR_SENSOR_ATTRIBUTES, | ||
ATTR_SENSOR_DEVICE_CLASS, ATTR_SENSOR_ICON, | ||
ATTR_SENSOR_NAME, ATTR_SENSOR_TYPE, ATTR_SENSOR_UNIQUE_ID, | ||
DOMAIN, SIGNAL_SENSOR_UPDATE) | ||
|
||
|
||
class MobileAppEntity(Entity): | ||
"""Representation of an mobile app entity.""" | ||
|
||
def __init__(self, config: dict, device: DeviceEntry, entry: ConfigEntry): | ||
"""Initialize the sensor.""" | ||
self._config = config | ||
self._device = device | ||
self._entry = entry | ||
self._registration = entry.data | ||
self._sensor_id = "{}_{}".format(self._registration[CONF_WEBHOOK_ID], | ||
config[ATTR_SENSOR_UNIQUE_ID]) | ||
self._entity_type = config[ATTR_SENSOR_TYPE] | ||
self.unsub_dispatcher = None | ||
|
||
async def async_added_to_hass(self): | ||
"""Register callbacks.""" | ||
self.unsub_dispatcher = async_dispatcher_connect(self.hass, | ||
SIGNAL_SENSOR_UPDATE, | ||
self._handle_update) | ||
|
||
async def async_will_remove_from_hass(self): | ||
"""Disconnect dispatcher listener when removed.""" | ||
if self.unsub_dispatcher is not None: | ||
self.unsub_dispatcher() | ||
|
||
@property | ||
def should_poll(self) -> bool: | ||
"""Declare that this entity pushes its state to HA.""" | ||
return False | ||
|
||
@property | ||
def name(self): | ||
"""Return the name of the mobile app sensor.""" | ||
return self._config[ATTR_SENSOR_NAME] | ||
|
||
@property | ||
def device_class(self): | ||
"""Return the device class.""" | ||
return self._config.get(ATTR_SENSOR_DEVICE_CLASS) | ||
|
||
@property | ||
def device_state_attributes(self): | ||
"""Return the device state attributes.""" | ||
return self._config[ATTR_SENSOR_ATTRIBUTES] | ||
|
||
@property | ||
def icon(self): | ||
"""Return the icon to use in the frontend, if any.""" | ||
return self._config[ATTR_SENSOR_ICON] | ||
|
||
@property | ||
def unique_id(self): | ||
"""Return the unique ID of this sensor.""" | ||
return self._sensor_id | ||
|
||
@property | ||
def device_info(self): | ||
"""Return device registry information for this entity.""" | ||
return { | ||
'identifiers': { | ||
(ATTR_DEVICE_ID, self._registration[ATTR_DEVICE_ID]), | ||
(CONF_WEBHOOK_ID, self._registration[CONF_WEBHOOK_ID]) | ||
}, | ||
'manufacturer': self._registration[ATTR_MANUFACTURER], | ||
'model': self._registration[ATTR_MODEL], | ||
'device_name': self._registration[ATTR_DEVICE_NAME], | ||
'sw_version': self._registration[ATTR_OS_VERSION], | ||
'config_entries': self._device.config_entries | ||
} | ||
|
||
async def async_update(self): | ||
"""Get the latest state of the sensor.""" | ||
data = self.hass.data[DOMAIN] | ||
try: | ||
self._config = data[self._entity_type][self._sensor_id] | ||
except KeyError: | ||
return | ||
|
||
@callback | ||
def _handle_update(self, data): | ||
"""Handle async event updates.""" | ||
self._config = data | ||
self.async_schedule_update_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
Oops, something went wrong.