From ec45e72bea9c72d461737502cd0dcba4a934f63f Mon Sep 17 00:00:00 2001 From: Thomas Germain <12560542+thomasgermain@users.noreply.github.com> Date: Wed, 13 Nov 2019 14:12:36 +0100 Subject: [PATCH] Add verisure ethernet status (#28656) --- .../components/verisure/binary_sensor.py | 36 +++++++++- .../verisure/test_ethernet_status.py | 67 +++++++++++++++++++ tests/components/verisure/test_lock.py | 10 ++- 3 files changed, 109 insertions(+), 4 deletions(-) create mode 100644 tests/components/verisure/test_ethernet_status.py diff --git a/homeassistant/components/verisure/binary_sensor.py b/homeassistant/components/verisure/binary_sensor.py index b7c8cbb49a3..47ec3c536b3 100644 --- a/homeassistant/components/verisure/binary_sensor.py +++ b/homeassistant/components/verisure/binary_sensor.py @@ -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 diff --git a/tests/components/verisure/test_ethernet_status.py b/tests/components/verisure/test_ethernet_status.py new file mode 100644 index 00000000000..71c7df94ae5 --- /dev/null +++ b/tests/components/verisure/test_ethernet_status.py @@ -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" diff --git a/tests/components/verisure/test_lock.py b/tests/components/verisure/test_lock.py index db277285819..ac03e0d9fb6 100644 --- a/tests/components/verisure/test_lock.py +++ b/tests/components/verisure/test_lock.py @@ -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):