Fix error if light status is missing in Nice G.O. (#126432)

This commit is contained in:
IceBotYT 2024-09-22 11:30:59 -04:00 committed by GitHub
parent 2a36ec3e21
commit f9e7721653
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 54 additions and 6 deletions

View File

@ -47,7 +47,7 @@ class NiceGODevice:
id: str
name: str
barrier_status: str
light_status: bool
light_status: bool | None
fw_version: str
connected: bool
vacation_mode: bool
@ -113,7 +113,11 @@ class NiceGOUpdateCoordinator(DataUpdateCoordinator[dict[str, NiceGODevice]]):
else:
barrier_status = BARRIER_STATUS[int(barrier_status_raw[2])].lower()
light_status = barrier_state.reported["lightStatus"].split(",")[0] == "1"
light_status = (
barrier_state.reported["lightStatus"].split(",")[0] == "1"
if barrier_state.reported.get("lightStatus")
else None
)
fw_version = barrier_state.reported["deviceFwVersion"]
if barrier_state.connectionState:
connected = barrier_state.connectionState.connected

View File

@ -1,6 +1,6 @@
"""Nice G.O. light."""
from typing import Any
from typing import TYPE_CHECKING, Any
from homeassistant.components.light import ColorMode, LightEntity
from homeassistant.core import HomeAssistant
@ -22,6 +22,7 @@ async def async_setup_entry(
async_add_entities(
NiceGOLightEntity(coordinator, device_id, device_data.name)
for device_id, device_data in coordinator.data.items()
if device_data.light_status is not None
)
@ -35,6 +36,8 @@ class NiceGOLightEntity(NiceGOEntity, LightEntity):
@property
def is_on(self) -> bool:
"""Return if the light is on or not."""
if TYPE_CHECKING:
assert self.data.light_status is not None
return self.data.light_status
async def async_turn_on(self, **kwargs: Any) -> None:

View File

@ -60,5 +60,35 @@
"connected": true,
"updatedTimestamp": "123"
}
},
{
"id": "3",
"type": "WallStation",
"controlLevel": "Owner",
"attr": [
{
"key": "organization",
"value": "test_organization"
}
],
"state": {
"deviceId": "3",
"desired": { "key": "value" },
"reported": {
"displayName": "Test Garage 3",
"autoDisabled": false,
"migrationStatus": "DONE",
"deviceId": "3",
"vcnMode": false,
"deviceFwVersion": "1.2.3.4.5.6",
"barrierStatus": "2,100,0,0,-1,0,3,0"
},
"timestamp": null,
"version": null
},
"connectionState": {
"connected": true,
"updatedTimestamp": "123"
}
}
]

View File

@ -120,11 +120,11 @@
'original_device_class': <CoverDeviceClass.GARAGE: 'garage'>,
'original_icon': None,
'original_name': None,
'platform': 'linear_garage_door',
'platform': 'nice_go',
'previous_unique_id': None,
'supported_features': <CoverEntityFeature: 3>,
'translation_key': None,
'unique_id': 'test3-GDO',
'unique_id': '3',
'unit_of_measurement': None,
})
# ---
@ -140,7 +140,7 @@
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'opening',
'state': 'closed',
})
# ---
# name: test_covers[cover.test_garage_4-entry]

View File

@ -20,6 +20,15 @@
'name': 'Test Garage 2',
'vacation_mode': True,
}),
'3': dict({
'barrier_status': 'closed',
'connected': True,
'fw_version': '1.2.3.4.5.6',
'id': '3',
'light_status': None,
'name': 'Test Garage 3',
'vacation_mode': False,
}),
}),
'entry': dict({
'data': dict({

View File

@ -78,6 +78,7 @@ async def test_update_light_state(
assert hass.states.get("light.test_garage_1_light").state == STATE_ON
assert hass.states.get("light.test_garage_2_light").state == STATE_OFF
assert hass.states.get("light.test_garage_3_light") is None
device_update = load_json_object_fixture("device_state_update.json", DOMAIN)
await mock_config_entry.runtime_data.on_data(device_update)
@ -86,3 +87,4 @@ async def test_update_light_state(
assert hass.states.get("light.test_garage_1_light").state == STATE_OFF
assert hass.states.get("light.test_garage_2_light").state == STATE_ON
assert hass.states.get("light.test_garage_3_light") is None