Rewrite rfxtrx init logic to do away with global object (#37699)
* Rewrite init logic to do away with global object * Put constant at end * Use a set instead of list for device_ids
This commit is contained in:
1 parent
b45a952d61
commit
67038c6ba8
11 files changed
+365
-334
No files matched your search
@@ -59,8 +59,6 @@ async def rfxtrx_cleanup():
|
||||
):
|
||||
yield
|
||||
|
||||
rfxtrx_core.RFX_DEVICES.clear()
|
||||
|
||||
|
||||
@pytest.fixture(name="rfxtrx")
|
||||
async def rfxtrx_fixture(hass):
|
||||
|
||||
@@ -141,7 +141,7 @@ async def test_fire_event(hass):
|
||||
assert state
|
||||
assert state.state == "on"
|
||||
|
||||
assert 1 == len(calls)
|
||||
assert len(calls) == 1
|
||||
assert calls[0].data == {"entity_id": "switch.test", "state": "on"}
|
||||
|
||||
|
||||
@@ -187,5 +187,5 @@ async def test_fire_event_sensor(hass):
|
||||
hass.bus.async_listen("signal_received", record_event)
|
||||
|
||||
await _signal_event(hass, "0a520802060101ff0f0269")
|
||||
assert 1 == len(calls)
|
||||
assert calls[0].data == {"entity_id": "sensor.test_temperature"}
|
||||
assert len(calls) == 5
|
||||
assert any(call.data == {"entity_id": "sensor.test_temperature"} for call in calls)
|
||||
@@ -57,7 +57,7 @@ async def test_default_config(hass, rfxtrx):
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert 0 == len(rfxtrx_core.RFX_DEVICES)
|
||||
assert len(hass.states.async_all()) == 0
|
||||
|
||||
|
||||
async def test_one_light(hass, rfxtrx):
|
||||
|
||||
@@ -55,12 +55,39 @@ async def test_one_sensor_no_datatype(hass, rfxtrx):
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("sensor.test_temperature")
|
||||
base_id = "sensor.test"
|
||||
base_name = "Test"
|
||||
|
||||
state = hass.states.get(f"{base_id}_temperature")
|
||||
assert state
|
||||
assert state.state == "unknown"
|
||||
assert state.attributes.get("friendly_name") == "Test Temperature"
|
||||
assert state.attributes.get("friendly_name") == f"{base_name} Temperature"
|
||||
assert state.attributes.get("unit_of_measurement") == TEMP_CELSIUS
|
||||
|
||||
state = hass.states.get(f"{base_id}_humidity")
|
||||
assert state
|
||||
assert state.state == "unknown"
|
||||
assert state.attributes.get("friendly_name") == f"{base_name} Humidity"
|
||||
assert state.attributes.get("unit_of_measurement") == UNIT_PERCENTAGE
|
||||
|
||||
state = hass.states.get(f"{base_id}_humidity_status")
|
||||
assert state
|
||||
assert state.state == "unknown"
|
||||
assert state.attributes.get("friendly_name") == f"{base_name} Humidity status"
|
||||
assert state.attributes.get("unit_of_measurement") == ""
|
||||
|
||||
state = hass.states.get(f"{base_id}_rssi_numeric")
|
||||
assert state
|
||||
assert state.state == "unknown"
|
||||
assert state.attributes.get("friendly_name") == f"{base_name} Rssi numeric"
|
||||
assert state.attributes.get("unit_of_measurement") == ""
|
||||
|
||||
state = hass.states.get(f"{base_id}_battery_numeric")
|
||||
assert state
|
||||
assert state.state == "unknown"
|
||||
assert state.attributes.get("friendly_name") == f"{base_name} Battery numeric"
|
||||
assert state.attributes.get("unit_of_measurement") == ""
|
||||
|
||||
|
||||
async def test_several_sensors(hass, rfxtrx):
|
||||
"""Test with 3 sensors."""
|
||||
@@ -113,61 +140,94 @@ async def test_discover_sensor(hass, rfxtrx):
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# 1
|
||||
await _signal_event(hass, "0a520801070100b81b0279")
|
||||
state = hass.states.get("sensor.0a520801070100b81b0279_temperature")
|
||||
base_id = "sensor.0a520801070100b81b0279"
|
||||
|
||||
state = hass.states.get(f"{base_id}_humidity")
|
||||
assert state
|
||||
assert state.state == "27"
|
||||
assert state.attributes.get("unit_of_measurement") == UNIT_PERCENTAGE
|
||||
|
||||
state = hass.states.get(f"{base_id}_humidity_status")
|
||||
assert state
|
||||
assert state.state == "normal"
|
||||
assert state.attributes.get("unit_of_measurement") == ""
|
||||
|
||||
state = hass.states.get(f"{base_id}_rssi_numeric")
|
||||
assert state
|
||||
assert state.state == "7"
|
||||
assert state.attributes.get("unit_of_measurement") == ""
|
||||
|
||||
state = hass.states.get(f"{base_id}_temperature")
|
||||
assert state
|
||||
assert state.state == "18.4"
|
||||
assert (
|
||||
state.attributes.items()
|
||||
>= {
|
||||
"friendly_name": "0a520801070100b81b0279 Temperature",
|
||||
"unit_of_measurement": TEMP_CELSIUS,
|
||||
"Humidity status": "normal",
|
||||
"Temperature": 18.4,
|
||||
"Rssi numeric": 7,
|
||||
"Humidity": 27,
|
||||
"Battery numeric": 9,
|
||||
"Humidity status numeric": 2,
|
||||
}.items()
|
||||
)
|
||||
assert state.attributes.get("unit_of_measurement") == TEMP_CELSIUS
|
||||
|
||||
state = hass.states.get(f"{base_id}_battery_numeric")
|
||||
assert state
|
||||
assert state.state == "9"
|
||||
assert state.attributes.get("unit_of_measurement") == ""
|
||||
|
||||
# 2
|
||||
await _signal_event(hass, "0a52080405020095240279")
|
||||
state = hass.states.get("sensor.0a52080405020095240279_temperature")
|
||||
base_id = "sensor.0a52080405020095240279"
|
||||
state = hass.states.get(f"{base_id}_humidity")
|
||||
|
||||
assert state
|
||||
assert state.state == "36"
|
||||
assert state.attributes.get("unit_of_measurement") == UNIT_PERCENTAGE
|
||||
|
||||
state = hass.states.get(f"{base_id}_humidity_status")
|
||||
assert state
|
||||
assert state.state == "normal"
|
||||
assert state.attributes.get("unit_of_measurement") == ""
|
||||
|
||||
state = hass.states.get(f"{base_id}_rssi_numeric")
|
||||
assert state
|
||||
assert state.state == "7"
|
||||
assert state.attributes.get("unit_of_measurement") == ""
|
||||
|
||||
state = hass.states.get(f"{base_id}_temperature")
|
||||
assert state
|
||||
assert state.state == "14.9"
|
||||
assert (
|
||||
state.attributes.items()
|
||||
>= {
|
||||
"friendly_name": "0a52080405020095240279 Temperature",
|
||||
"unit_of_measurement": TEMP_CELSIUS,
|
||||
"Humidity status": "normal",
|
||||
"Temperature": 14.9,
|
||||
"Rssi numeric": 7,
|
||||
"Humidity": 36,
|
||||
"Battery numeric": 9,
|
||||
"Humidity status numeric": 2,
|
||||
}.items()
|
||||
)
|
||||
assert state.attributes.get("unit_of_measurement") == TEMP_CELSIUS
|
||||
|
||||
state = hass.states.get(f"{base_id}_battery_numeric")
|
||||
assert state
|
||||
assert state.state == "9"
|
||||
assert state.attributes.get("unit_of_measurement") == ""
|
||||
|
||||
# 1 Update
|
||||
await _signal_event(hass, "0a52085e070100b31b0279")
|
||||
state = hass.states.get("sensor.0a520801070100b81b0279_temperature")
|
||||
base_id = "sensor.0a520801070100b81b0279"
|
||||
|
||||
state = hass.states.get(f"{base_id}_humidity")
|
||||
assert state
|
||||
assert state.state == "27"
|
||||
assert state.attributes.get("unit_of_measurement") == UNIT_PERCENTAGE
|
||||
|
||||
state = hass.states.get(f"{base_id}_humidity_status")
|
||||
assert state
|
||||
assert state.state == "normal"
|
||||
assert state.attributes.get("unit_of_measurement") == ""
|
||||
|
||||
state = hass.states.get(f"{base_id}_rssi_numeric")
|
||||
assert state
|
||||
assert state.state == "7"
|
||||
assert state.attributes.get("unit_of_measurement") == ""
|
||||
|
||||
state = hass.states.get(f"{base_id}_temperature")
|
||||
assert state
|
||||
assert state.state == "17.9"
|
||||
assert (
|
||||
state.attributes.items()
|
||||
>= {
|
||||
"friendly_name": "0a520801070100b81b0279 Temperature",
|
||||
"unit_of_measurement": TEMP_CELSIUS,
|
||||
"Humidity status": "normal",
|
||||
"Temperature": 17.9,
|
||||
"Rssi numeric": 7,
|
||||
"Humidity": 27,
|
||||
"Battery numeric": 9,
|
||||
"Humidity status numeric": 2,
|
||||
}.items()
|
||||
)
|
||||
assert state.attributes.get("unit_of_measurement") == TEMP_CELSIUS
|
||||
|
||||
assert len(hass.states.async_all()) == 2
|
||||
state = hass.states.get(f"{base_id}_battery_numeric")
|
||||
assert state
|
||||
assert state.state == "9"
|
||||
assert state.attributes.get("unit_of_measurement") == ""
|
||||
|
||||
assert len(hass.states.async_all()) == 10
|
||||
|
||||
|
||||
async def test_discover_sensor_noautoadd(hass, rfxtrx):
|
||||
@@ -215,35 +275,14 @@ async def test_update_of_sensors(hass, rfxtrx):
|
||||
state = hass.states.get("sensor.test_temperature")
|
||||
assert state
|
||||
assert state.state == "unknown"
|
||||
assert (
|
||||
state.attributes.items()
|
||||
>= {
|
||||
"friendly_name": "Test Temperature",
|
||||
"unit_of_measurement": TEMP_CELSIUS,
|
||||
}.items()
|
||||
)
|
||||
|
||||
state = hass.states.get("sensor.bath_temperature")
|
||||
assert state
|
||||
assert state.state == "unknown"
|
||||
assert (
|
||||
state.attributes.items()
|
||||
>= {
|
||||
"friendly_name": "Bath Temperature",
|
||||
"unit_of_measurement": TEMP_CELSIUS,
|
||||
}.items()
|
||||
)
|
||||
|
||||
state = hass.states.get("sensor.bath_humidity")
|
||||
assert state
|
||||
assert state.state == "unknown"
|
||||
assert (
|
||||
state.attributes.items()
|
||||
>= {
|
||||
"friendly_name": "Bath Humidity",
|
||||
"unit_of_measurement": UNIT_PERCENTAGE,
|
||||
}.items()
|
||||
)
|
||||
|
||||
assert len(hass.states.async_all()) == 3
|
||||
|
||||
@@ -253,52 +292,13 @@ async def test_update_of_sensors(hass, rfxtrx):
|
||||
state = hass.states.get("sensor.test_temperature")
|
||||
assert state
|
||||
assert state.state == "13.3"
|
||||
assert (
|
||||
state.attributes.items()
|
||||
>= {
|
||||
"friendly_name": "Test Temperature",
|
||||
"unit_of_measurement": TEMP_CELSIUS,
|
||||
"Battery numeric": 9,
|
||||
"Temperature": 13.3,
|
||||
"Humidity": 34,
|
||||
"Humidity status": "normal",
|
||||
"Humidity status numeric": 2,
|
||||
"Rssi numeric": 6,
|
||||
}.items()
|
||||
)
|
||||
|
||||
state = hass.states.get("sensor.bath_temperature")
|
||||
assert state
|
||||
assert state.state == "51.1"
|
||||
assert (
|
||||
state.attributes.items()
|
||||
>= {
|
||||
"friendly_name": "Bath Temperature",
|
||||
"unit_of_measurement": TEMP_CELSIUS,
|
||||
"Battery numeric": 9,
|
||||
"Temperature": 51.1,
|
||||
"Humidity": 15,
|
||||
"Humidity status": "normal",
|
||||
"Humidity status numeric": 2,
|
||||
"Rssi numeric": 6,
|
||||
}.items()
|
||||
)
|
||||
|
||||
state = hass.states.get("sensor.bath_humidity")
|
||||
assert state
|
||||
assert state.state == "15"
|
||||
assert (
|
||||
state.attributes.items()
|
||||
>= {
|
||||
"friendly_name": "Bath Humidity",
|
||||
"unit_of_measurement": UNIT_PERCENTAGE,
|
||||
"Battery numeric": 9,
|
||||
"Temperature": 51.1,
|
||||
"Humidity": 15,
|
||||
"Humidity status": "normal",
|
||||
"Humidity status numeric": 2,
|
||||
"Rssi numeric": 6,
|
||||
}.items()
|
||||
)
|
||||
|
||||
assert len(hass.states.async_all()) == 3
|
||||
Reference in new issue
Block a user