Skip to content

Commit

Permalink
feat: add missing tests for binary_sensor entities (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
xtimmy86x authored Oct 20, 2023
1 parent a39d448 commit e8c4c56
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from custom_components.econnect_metronet import async_setup
from custom_components.econnect_metronet.alarm_control_panel import EconnectAlarm
from custom_components.econnect_metronet.const import DOMAIN
from custom_components.econnect_metronet.coordinator import AlarmCoordinator
from custom_components.econnect_metronet.devices import AlarmDevice

Expand All @@ -25,6 +26,7 @@ async def hass(hass):
"""
await async_setup(hass, {})
hass.data["custom_components"] = None
hass.data[DOMAIN]["test_entry_id"] = {}
yield hass


Expand Down Expand Up @@ -139,6 +141,7 @@ def __init__(self):
}

# Options at configuration-time
self.entry_id = "test_entry_id"
self.options = {}

return MockConfigEntry()
8 changes: 8 additions & 0 deletions tests/fixtures/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ def test_client_get_sectors_status(server):
"Created": "/Date(1546004147490+0100)/",
"Version": "AAAAAAAAgRs="
},
{
"AccountId": 1,
"Class": 9,
"Index": 3,
"Description": "S4 Garage",
"Created": "/Date(1546004147491+0100)/",
"Version": "AAAAAAAAgRt="
},
{
"AccountId": 1,
"Class": 10,
Expand Down
73 changes: 73 additions & 0 deletions tests/test_binary_sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,56 @@
AlertSensor,
InputSensor,
SectorSensor,
async_setup_entry,
)
from custom_components.econnect_metronet.const import DOMAIN


@pytest.mark.asyncio
async def test_async_setup_entry_in_use(hass, config_entry, alarm_device, coordinator):
# Ensure the async setup loads only sectors and sensors that are in use
hass.data[DOMAIN][config_entry.entry_id] = {
"device": alarm_device,
"coordinator": coordinator,
}

# Test
def ensure_only_in_use(sensors):
assert len(sensors) == 31

await async_setup_entry(hass, config_entry, ensure_only_in_use)


@pytest.mark.asyncio
async def test_async_setup_entry_unused_input(hass, config_entry, alarm_device, coordinator):
# Ensure the async setup don't load sensors that are not in use
hass.data[DOMAIN][config_entry.entry_id] = {
"device": alarm_device,
"coordinator": coordinator,
}

# Test
def ensure_unused_sensors(sensors):
for sensor in sensors:
assert sensor._name not in ["Outdoor Sensor 3"]

await async_setup_entry(hass, config_entry, ensure_unused_sensors)


@pytest.mark.asyncio
async def test_async_setup_entry_unused_sector(hass, config_entry, alarm_device, coordinator):
# Ensure the async setup don't load sectors that are not in use
hass.data[DOMAIN][config_entry.entry_id] = {
"device": alarm_device,
"coordinator": coordinator,
}

# Test
def ensure_unused_sectors(sensors):
for sensor in sensors:
assert sensor._name not in ["S4 Garage"]

await async_setup_entry(hass, config_entry, ensure_unused_sectors)


class TestAlertSensor:
Expand Down Expand Up @@ -69,12 +118,24 @@ def test_binary_sensor_entity_id_with_system_name(self, hass, config_entry, alar
entity = AlertSensor("test_id", 0, config_entry, "has_anomalies", coordinator, alarm_device)
assert entity.entity_id == "econnect_metronet.econnect_metronet_home_has_anomalies"

def test_binary_sensor_unique_id(self, hass, config_entry, alarm_device):
# Ensure the alert has the right unique ID
coordinator = DataUpdateCoordinator(hass, logging.getLogger(__name__), name="econnect_metronet")
entity = AlertSensor("test_id", 0, config_entry, "has_anomalies", coordinator, alarm_device)
assert entity.unique_id == "test_id"

def test_binary_sensor_icon(self, hass, config_entry, alarm_device):
# Ensure the sensor has the right icon
coordinator = DataUpdateCoordinator(hass, logging.getLogger(__name__), name="econnect_metronet")
entity = AlertSensor("test_id", 0, config_entry, "has_anomalies", coordinator, alarm_device)
assert entity.icon == "hass:alarm-light"

def test_binary_sensor_device_class(self, hass, config_entry, alarm_device):
# Ensure the sensor has the right device class
coordinator = DataUpdateCoordinator(hass, logging.getLogger(__name__), name="econnect_metronet")
entity = AlertSensor("test_id", 0, config_entry, "has_anomalies", coordinator, alarm_device)
assert entity.device_class == "problem"


class TestInputSensor:
def test_binary_sensor_name(self, hass, config_entry, alarm_device):
Expand Down Expand Up @@ -103,6 +164,12 @@ def test_binary_sensor_entity_id_with_system_name(self, hass, config_entry, alar
entity = InputSensor("test_id", 1, config_entry, "1 Tamper Sirena", coordinator, alarm_device)
assert entity.entity_id == "econnect_metronet.econnect_metronet_home_1_tamper_sirena"

def test_binary_sensor_unique_id(self, hass, config_entry, alarm_device):
# Ensure the sensor has the right unique ID
coordinator = DataUpdateCoordinator(hass, logging.getLogger(__name__), name="econnect_metronet")
entity = InputSensor("test_id", 1, config_entry, "1 Tamper Sirena", coordinator, alarm_device)
assert entity.unique_id == "test_id"

def test_binary_sensor_icon(self, hass, config_entry, alarm_device):
# Ensure the sensor has the right icon
coordinator = DataUpdateCoordinator(hass, logging.getLogger(__name__), name="econnect_metronet")
Expand Down Expand Up @@ -149,6 +216,12 @@ def test_binary_sensor_input_entity_id_with_system_name(self, hass, config_entry
entity = SectorSensor("test_id", 1, config_entry, "1 S1 Living Room", coordinator, alarm_device)
assert entity.entity_id == "econnect_metronet.econnect_metronet_home_1_s1_living_room"

def test_binary_sensor_input_unique_id(self, hass, config_entry, alarm_device):
# Ensure the sensor has the right unique ID
coordinator = DataUpdateCoordinator(hass, logging.getLogger(__name__), name="econnect_metronet")
entity = SectorSensor("test_id", 1, config_entry, "1 S1 Living Room", coordinator, alarm_device)
assert entity.unique_id == "test_id"

def test_binary_sensor_icon(self, hass, config_entry, alarm_device):
# Ensure the sensor has the right icon
coordinator = DataUpdateCoordinator(hass, logging.getLogger(__name__), name="econnect_metronet")
Expand Down

0 comments on commit e8c4c56

Please sign in to comment.