Add verisure ethernet status (#28656)

This commit is contained in:
Thomas Germain 2019-11-13 14:12:36 +01:00 committed by Martin Hjelmare
parent 8f390d099a
commit ec45e72bea
3 changed files with 109 additions and 4 deletions

View file

@ -1,7 +1,10 @@
"""Support for Verisure binary sensors."""
import logging
from homeassistant.components.binary_sensor import BinarySensorDevice
from homeassistant.components.binary_sensor import (
BinarySensorDevice,
DEVICE_CLASS_CONNECTIVITY,
)
from . import CONF_DOOR_WINDOW, HUB as hub
@ -22,6 +25,8 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
)
]
)
sensors.extend([VerisureEthernetStatus()])
add_entities(sensors)
@ -66,3 +71,32 @@ class VerisureDoorWindowSensor(BinarySensorDevice):
def update(self):
"""Update the state of the sensor."""
hub.update_overview()
class VerisureEthernetStatus(BinarySensorDevice):
"""Representation of a Verisure VBOX internet status."""
@property
def name(self):
"""Return the name of the binary sensor."""
return "Verisure Ethernet status"
@property
def is_on(self):
"""Return the state of the sensor."""
return hub.get_first("$.ethernetConnectedNow")
@property
def available(self):
"""Return True if entity is available."""
return hub.get_first("$.ethernetConnectedNow") is not None
# pylint: disable=no-self-use
def update(self):
"""Update the state of the sensor."""
hub.update_overview()
@property
def device_class(self):
"""Return the class of this device, from component DEVICE_CLASSES."""
return DEVICE_CLASS_CONNECTIVITY

View file

@ -0,0 +1,67 @@
"""Test Verisure ethernet status."""
from contextlib import contextmanager
from unittest.mock import patch
from homeassistant.const import STATE_UNAVAILABLE
from homeassistant.setup import async_setup_component
from homeassistant.components.verisure import DOMAIN as VERISURE_DOMAIN
CONFIG = {
"verisure": {
"username": "test",
"password": "test",
"alarm": False,
"door_window": False,
"hygrometers": False,
"mouse": False,
"smartplugs": False,
"thermometers": False,
"smartcam": False,
}
}
@contextmanager
def mock_hub(config, response):
"""Extensively mock out a verisure hub."""
hub_prefix = "homeassistant.components.verisure.binary_sensor.hub"
verisure_prefix = "verisure.Session"
with patch(verisure_prefix) as session, patch(hub_prefix) as hub:
session.login.return_value = True
hub.config = config["verisure"]
hub.get.return_value = response
hub.get_first.return_value = response.get("ethernetConnectedNow", None)
yield hub
async def setup_verisure(hass, config, response):
"""Set up mock verisure."""
with mock_hub(config, response):
await async_setup_component(hass, VERISURE_DOMAIN, config)
await hass.async_block_till_done()
async def test_verisure_no_ethernet_status(hass):
"""Test no data from API."""
await setup_verisure(hass, CONFIG, {})
assert len(hass.states.async_all()) == 1
entity_id = hass.states.async_entity_ids()[0]
assert hass.states.get(entity_id).state == STATE_UNAVAILABLE
async def test_verisure_ethernet_status_disconnected(hass):
"""Test disconnected."""
await setup_verisure(hass, CONFIG, {"ethernetConnectedNow": False})
assert len(hass.states.async_all()) == 1
entity_id = hass.states.async_entity_ids()[0]
assert hass.states.get(entity_id).state == "off"
async def test_verisure_ethernet_status_connected(hass):
"""Test connected."""
await setup_verisure(hass, CONFIG, {"ethernetConnectedNow": True})
assert len(hass.states.async_all()) == 1
entity_id = hass.states.async_entity_ids()[0]
assert hass.states.get(entity_id).state == "on"

View file

@ -50,6 +50,9 @@ LOCKS = ["door_lock"]
def mock_hub(config, get_response=LOCKS[0]):
"""Extensively mock out a verisure hub."""
hub_prefix = "homeassistant.components.verisure.lock.hub"
# Since there is no conf to disable ethernet status, mock hub for
# binary sensor too
hub_binary_sensor = "homeassistant.components.verisure.binary_sensor.hub"
verisure_prefix = "verisure.Session"
with patch(verisure_prefix) as session, patch(hub_prefix) as hub:
session.login.return_value = True
@ -62,7 +65,8 @@ def mock_hub(config, get_response=LOCKS[0]):
}
hub.session.get_lock_state_transaction.return_value = {"result": "OK"}
yield hub
with patch(hub_binary_sensor, hub):
yield hub
async def setup_verisure_locks(hass, config):
@ -70,8 +74,8 @@ async def setup_verisure_locks(hass, config):
with mock_hub(config):
await async_setup_component(hass, VERISURE_DOMAIN, config)
await hass.async_block_till_done()
# lock.door_lock, group.all_locks
assert len(hass.states.async_all()) == 2
# lock.door_lock, group.all_locks, ethernet_status
assert len(hass.states.async_all()) == 3
async def test_verisure_no_default_code(hass):