forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Complete mysensors sensor coverage (home-assistant#54471)
- Loading branch information
1 parent
1e14b3a
commit 028a3d3
Showing
7 changed files
with
301 additions
and
14 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
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 |
---|---|---|
@@ -1,31 +1,161 @@ | ||
"""Provide tests for mysensors sensor platform.""" | ||
from __future__ import annotations | ||
|
||
from typing import Callable | ||
|
||
from homeassistant.components.sensor import ATTR_STATE_CLASS, STATE_CLASS_MEASUREMENT | ||
from mysensors.sensor import Sensor | ||
import pytest | ||
|
||
from homeassistant.components.sensor import ( | ||
ATTR_LAST_RESET, | ||
ATTR_STATE_CLASS, | ||
STATE_CLASS_MEASUREMENT, | ||
) | ||
from homeassistant.const import ( | ||
ATTR_DEVICE_CLASS, | ||
ATTR_ICON, | ||
ATTR_UNIT_OF_MEASUREMENT, | ||
DEVICE_CLASS_ENERGY, | ||
DEVICE_CLASS_POWER, | ||
DEVICE_CLASS_TEMPERATURE, | ||
ENERGY_KILO_WATT_HOUR, | ||
POWER_WATT, | ||
TEMP_CELSIUS, | ||
TEMP_FAHRENHEIT, | ||
) | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.util.dt import utc_from_timestamp | ||
from homeassistant.util.unit_system import IMPERIAL_SYSTEM, METRIC_SYSTEM, UnitSystem | ||
|
||
from tests.common import MockConfigEntry | ||
|
||
async def test_gps_sensor(hass, gps_sensor, integration): | ||
|
||
async def test_gps_sensor( | ||
hass: HomeAssistant, | ||
gps_sensor: Sensor, | ||
integration: tuple[MockConfigEntry, Callable[[str], None]], | ||
) -> None: | ||
"""Test a gps sensor.""" | ||
entity_id = "sensor.gps_sensor_1_1" | ||
_, receive_message = integration | ||
|
||
state = hass.states.get(entity_id) | ||
|
||
assert state | ||
assert state.state == "40.741894,-73.989311,12" | ||
|
||
altitude = 0 | ||
new_coords = "40.782,-73.965" | ||
message_string = f"1;1;1;0;49;{new_coords},{altitude}\n" | ||
|
||
receive_message(message_string) | ||
# the integration adds multiple jobs to do the update currently | ||
await hass.async_block_till_done() | ||
await hass.async_block_till_done() | ||
await hass.async_block_till_done() | ||
|
||
state = hass.states.get(entity_id) | ||
|
||
assert state | ||
assert state.state == f"{new_coords},{altitude}" | ||
|
||
|
||
async def test_power_sensor(hass, power_sensor, integration): | ||
async def test_power_sensor( | ||
hass: HomeAssistant, | ||
power_sensor: Sensor, | ||
integration: tuple[MockConfigEntry, Callable[[str], None]], | ||
) -> None: | ||
"""Test a power sensor.""" | ||
entity_id = "sensor.power_sensor_1_1" | ||
|
||
state = hass.states.get(entity_id) | ||
|
||
assert state | ||
assert state.state == "1200" | ||
assert state.attributes[ATTR_DEVICE_CLASS] == DEVICE_CLASS_POWER | ||
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == POWER_WATT | ||
assert state.attributes[ATTR_STATE_CLASS] == STATE_CLASS_MEASUREMENT | ||
assert ATTR_LAST_RESET not in state.attributes | ||
|
||
|
||
async def test_energy_sensor( | ||
hass: HomeAssistant, | ||
energy_sensor: Sensor, | ||
integration: tuple[MockConfigEntry, Callable[[str], None]], | ||
) -> None: | ||
"""Test an energy sensor.""" | ||
entity_id = "sensor.energy_sensor_1_1" | ||
|
||
state = hass.states.get(entity_id) | ||
|
||
assert state | ||
assert state.state == "18000" | ||
assert state.attributes[ATTR_DEVICE_CLASS] == DEVICE_CLASS_ENERGY | ||
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == ENERGY_KILO_WATT_HOUR | ||
assert state.attributes[ATTR_STATE_CLASS] == STATE_CLASS_MEASUREMENT | ||
assert state.attributes[ATTR_LAST_RESET] == utc_from_timestamp(0).isoformat() | ||
|
||
|
||
async def test_sound_sensor( | ||
hass: HomeAssistant, | ||
sound_sensor: Sensor, | ||
integration: tuple[MockConfigEntry, Callable[[str], None]], | ||
) -> None: | ||
"""Test a sound sensor.""" | ||
entity_id = "sensor.sound_sensor_1_1" | ||
|
||
state = hass.states.get(entity_id) | ||
|
||
assert state | ||
assert state.state == "10" | ||
assert state.attributes[ATTR_ICON] == "mdi:volume-high" | ||
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == "dB" | ||
|
||
|
||
async def test_distance_sensor( | ||
hass: HomeAssistant, | ||
distance_sensor: Sensor, | ||
integration: tuple[MockConfigEntry, Callable[[str], None]], | ||
) -> None: | ||
"""Test a distance sensor.""" | ||
entity_id = "sensor.distance_sensor_1_1" | ||
|
||
state = hass.states.get(entity_id) | ||
|
||
assert state | ||
assert state.state == "15" | ||
assert state.attributes[ATTR_ICON] == "mdi:ruler" | ||
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == "cm" | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"unit_system, unit", | ||
[(METRIC_SYSTEM, TEMP_CELSIUS), (IMPERIAL_SYSTEM, TEMP_FAHRENHEIT)], | ||
) | ||
async def test_temperature_sensor( | ||
hass: HomeAssistant, | ||
temperature_sensor: Sensor, | ||
integration: tuple[MockConfigEntry, Callable[[str], None]], | ||
unit_system: UnitSystem, | ||
unit: str, | ||
) -> None: | ||
"""Test a temperature sensor.""" | ||
entity_id = "sensor.temperature_sensor_1_1" | ||
hass.config.units = unit_system | ||
_, receive_message = integration | ||
temperature = "22.0" | ||
message_string = f"1;1;1;0;0;{temperature}\n" | ||
|
||
receive_message(message_string) | ||
# the integration adds multiple jobs to do the update currently | ||
await hass.async_block_till_done() | ||
await hass.async_block_till_done() | ||
await hass.async_block_till_done() | ||
|
||
state = hass.states.get(entity_id) | ||
|
||
assert state | ||
assert state.state == temperature | ||
assert state.attributes[ATTR_DEVICE_CLASS] == DEVICE_CLASS_TEMPERATURE | ||
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == unit | ||
assert state.attributes[ATTR_STATE_CLASS] == STATE_CLASS_MEASUREMENT |
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,22 @@ | ||
{ | ||
"1": { | ||
"sensor_id": 1, | ||
"children": { | ||
"1": { | ||
"id": 1, | ||
"type": 15, | ||
"description": "", | ||
"values": { | ||
"13": "15", | ||
"43": "cm" | ||
} | ||
} | ||
}, | ||
"type": 17, | ||
"sketch_name": "Distance Sensor", | ||
"sketch_version": "1.0", | ||
"battery_level": 0, | ||
"protocol_version": "2.3.2", | ||
"heartbeat": 0 | ||
} | ||
} |
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,21 @@ | ||
{ | ||
"1": { | ||
"sensor_id": 1, | ||
"children": { | ||
"1": { | ||
"id": 1, | ||
"type": 13, | ||
"description": "", | ||
"values": { | ||
"18": "18000" | ||
} | ||
} | ||
}, | ||
"type": 17, | ||
"sketch_name": "Energy Sensor", | ||
"sketch_version": "1.0", | ||
"battery_level": 0, | ||
"protocol_version": "2.3.2", | ||
"heartbeat": 0 | ||
} | ||
} |
Oops, something went wrong.