-
-
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.
Binary Sensor for Remote UI & Fix timezone (#22076)
* Binary Sensor for Remote UI * Fix lint * Revert make hass public * Add tests
- Loading branch information
Showing
7 changed files
with
124 additions
and
6 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,73 @@ | ||
"""Support for Home Assistant Cloud binary sensors.""" | ||
from homeassistant.components.binary_sensor import BinarySensorDevice | ||
from homeassistant.core import callback | ||
from homeassistant.helpers.dispatcher import async_dispatcher_connect | ||
|
||
from .const import DISPATCHER_REMOTE_UPDATE, DOMAIN | ||
|
||
DEPENDENCIES = ['cloud'] | ||
|
||
|
||
async def async_setup_platform( | ||
hass, config, async_add_entities, discovery_info=None): | ||
"""Set up the cloud binary sensors.""" | ||
if discovery_info is None: | ||
return | ||
cloud = hass.data[DOMAIN] | ||
|
||
async_add_entities([CloudRemoteBinary(cloud)]) | ||
|
||
|
||
class CloudRemoteBinary(BinarySensorDevice): | ||
"""Representation of an Cloud Remote UI Connection binary sensor.""" | ||
|
||
def __init__(self, cloud): | ||
"""Initialize the binary sensor.""" | ||
self.cloud = cloud | ||
self._unsub_dispatcher = None | ||
|
||
@property | ||
def name(self) -> str: | ||
"""Return the name of the binary sensor, if any.""" | ||
return "Remote UI" | ||
|
||
@property | ||
def unique_id(self) -> str: | ||
"""Return a unique ID.""" | ||
return "cloud-remote-ui-connectivity" | ||
|
||
@property | ||
def is_on(self) -> bool: | ||
"""Return true if the binary sensor is on.""" | ||
return self.cloud.remote.is_connected | ||
|
||
@property | ||
def device_class(self) -> str: | ||
"""Return the class of this device, from component DEVICE_CLASSES.""" | ||
return 'connectivity' | ||
|
||
@property | ||
def available(self) -> bool: | ||
"""Return True if entity is available.""" | ||
return self.cloud.remote.certificate is not None | ||
|
||
@property | ||
def should_poll(self) -> bool: | ||
"""Return True if entity has to be polled for state.""" | ||
return False | ||
|
||
async def async_added_to_hass(self): | ||
"""Register update dispatcher.""" | ||
@callback | ||
def async_state_update(data): | ||
"""Update callback.""" | ||
self.async_write_ha_state() | ||
|
||
self._unsub_dispatcher = async_dispatcher_connect( | ||
self.hass, DISPATCHER_REMOTE_UPDATE, async_state_update) | ||
|
||
async def async_will_remove_from_hass(self): | ||
"""Register update dispatcher.""" | ||
if self._unsub_dispatcher is not None: | ||
self._unsub_dispatcher() | ||
self._unsub_dispatcher = None |
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 |
---|---|---|
|
@@ -25,3 +25,5 @@ | |
|
||
MODE_DEV = "development" | ||
MODE_PROD = "production" | ||
|
||
DISPATCHER_REMOTE_UPDATE = 'cloud_remote_update' |
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,32 @@ | ||
"""Tests for the cloud binary sensor.""" | ||
from unittest.mock import Mock | ||
|
||
from homeassistant.setup import async_setup_component | ||
from homeassistant.components.cloud.const import DISPATCHER_REMOTE_UPDATE | ||
|
||
|
||
async def test_remote_connection_sensor(hass): | ||
"""Test the remote connection sensor.""" | ||
assert await async_setup_component(hass, 'cloud', {'cloud': {}}) | ||
cloud = hass.data['cloud'] = Mock() | ||
cloud.remote.certificate = None | ||
await hass.async_block_till_done() | ||
|
||
state = hass.states.get('binary_sensor.remote_ui') | ||
assert state is not None | ||
assert state.state == 'unavailable' | ||
|
||
cloud.remote.is_connected = False | ||
cloud.remote.certificate = object() | ||
hass.helpers.dispatcher.async_dispatcher_send(DISPATCHER_REMOTE_UPDATE, {}) | ||
await hass.async_block_till_done() | ||
|
||
state = hass.states.get('binary_sensor.remote_ui') | ||
assert state.state == 'off' | ||
|
||
cloud.remote.is_connected = True | ||
hass.helpers.dispatcher.async_dispatcher_send(DISPATCHER_REMOTE_UPDATE, {}) | ||
await hass.async_block_till_done() | ||
|
||
state = hass.states.get('binary_sensor.remote_ui') | ||
assert state.state == 'on' |