Skip to content
Snippets Groups Projects
Commit ec45e72b authored by Thomas Germain's avatar Thomas Germain Committed by Martin Hjelmare
Browse files

Add verisure ethernet status (#28656)

parent 8f390d09
Branches
Tags
No related merge requests found
"""Support for Verisure binary sensors.""" """Support for Verisure binary sensors."""
import logging 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 from . import CONF_DOOR_WINDOW, HUB as hub
...@@ -22,6 +25,8 @@ def setup_platform(hass, config, add_entities, discovery_info=None): ...@@ -22,6 +25,8 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
) )
] ]
) )
sensors.extend([VerisureEthernetStatus()])
add_entities(sensors) add_entities(sensors)
...@@ -66,3 +71,32 @@ class VerisureDoorWindowSensor(BinarySensorDevice): ...@@ -66,3 +71,32 @@ class VerisureDoorWindowSensor(BinarySensorDevice):
def update(self): def update(self):
"""Update the state of the sensor.""" """Update the state of the sensor."""
hub.update_overview() 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
"""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"
...@@ -50,6 +50,9 @@ LOCKS = ["door_lock"] ...@@ -50,6 +50,9 @@ LOCKS = ["door_lock"]
def mock_hub(config, get_response=LOCKS[0]): def mock_hub(config, get_response=LOCKS[0]):
"""Extensively mock out a verisure hub.""" """Extensively mock out a verisure hub."""
hub_prefix = "homeassistant.components.verisure.lock.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" verisure_prefix = "verisure.Session"
with patch(verisure_prefix) as session, patch(hub_prefix) as hub: with patch(verisure_prefix) as session, patch(hub_prefix) as hub:
session.login.return_value = True session.login.return_value = True
...@@ -62,7 +65,8 @@ def mock_hub(config, get_response=LOCKS[0]): ...@@ -62,7 +65,8 @@ def mock_hub(config, get_response=LOCKS[0]):
} }
hub.session.get_lock_state_transaction.return_value = {"result": "OK"} 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): async def setup_verisure_locks(hass, config):
...@@ -70,8 +74,8 @@ async def setup_verisure_locks(hass, config): ...@@ -70,8 +74,8 @@ async def setup_verisure_locks(hass, config):
with mock_hub(config): with mock_hub(config):
await async_setup_component(hass, VERISURE_DOMAIN, config) await async_setup_component(hass, VERISURE_DOMAIN, config)
await hass.async_block_till_done() await hass.async_block_till_done()
# lock.door_lock, group.all_locks # lock.door_lock, group.all_locks, ethernet_status
assert len(hass.states.async_all()) == 2 assert len(hass.states.async_all()) == 3
async def test_verisure_no_default_code(hass): async def test_verisure_no_default_code(hass):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment