diff --git a/homeassistant/components/litejet/scene.py b/homeassistant/components/litejet/scene.py index 762e8e543d5..c6d4817ccac 100644 --- a/homeassistant/components/litejet/scene.py +++ b/homeassistant/components/litejet/scene.py @@ -56,7 +56,7 @@ class LiteJetScene(Scene): def _on_connected_changed(self, connected: bool, reason: str) -> None: self._attr_available = connected - self.schedule_update_ha_state() + self.async_write_ha_state() @property def extra_state_attributes(self): diff --git a/homeassistant/components/litejet/switch.py b/homeassistant/components/litejet/switch.py index 1f010691f02..c2dcebd3261 100644 --- a/homeassistant/components/litejet/switch.py +++ b/homeassistant/components/litejet/switch.py @@ -56,17 +56,17 @@ class LiteJetSwitch(SwitchEntity): self._lj.unsubscribe(self._on_switch_released) self._lj.unsubscribe(self._on_connected_changed) - def _on_switch_pressed(self): + def _on_switch_pressed(self) -> None: self._attr_is_on = True - self.schedule_update_ha_state() + self.async_write_ha_state() - def _on_switch_released(self): + def _on_switch_released(self) -> None: self._attr_is_on = False - self.schedule_update_ha_state() + self.async_write_ha_state() def _on_connected_changed(self, connected: bool, reason: str) -> None: self._attr_available = connected - self.schedule_update_ha_state() + self.async_write_ha_state() @property def unique_id(self): diff --git a/tests/components/litejet/conftest.py b/tests/components/litejet/conftest.py index 4484c198d3c..3bbd9ef4ef0 100644 --- a/tests/components/litejet/conftest.py +++ b/tests/components/litejet/conftest.py @@ -27,6 +27,7 @@ def mock_litejet(): mock_lj.switch_released_callbacks = {} mock_lj.load_activated_callbacks = {} mock_lj.load_deactivated_callbacks = {} + mock_lj.connected_changed_callbacks = [] def on_switch_pressed(number, callback): mock_lj.switch_pressed_callbacks[number] = callback @@ -40,10 +41,14 @@ def mock_litejet(): def on_load_deactivated(number, callback): mock_lj.load_deactivated_callbacks[number] = callback + def on_connected_changed(callback): + mock_lj.connected_changed_callbacks.append(callback) + mock_lj.on_switch_pressed.side_effect = on_switch_pressed mock_lj.on_switch_released.side_effect = on_switch_released mock_lj.on_load_activated.side_effect = on_load_activated mock_lj.on_load_deactivated.side_effect = on_load_deactivated + mock_lj.on_connected_changed.side_effect = on_connected_changed mock_lj.open = AsyncMock() mock_lj.close = AsyncMock() @@ -70,4 +75,11 @@ def mock_litejet(): mock_lj.last_delta = timedelta(0) mock_lj.connected = True + def connected_changed(connected: bool, reason: str) -> None: + mock_lj.connected = connected + for callback in mock_lj.connected_changed_callbacks: + callback(connected, reason) + + mock_lj.connected_changed = connected_changed + yield mock_lj diff --git a/tests/components/litejet/test_light.py b/tests/components/litejet/test_light.py index af990de6f72..dd3a905d509 100644 --- a/tests/components/litejet/test_light.py +++ b/tests/components/litejet/test_light.py @@ -2,7 +2,13 @@ from homeassistant.components import light from homeassistant.components.light import ATTR_BRIGHTNESS, ATTR_TRANSITION from homeassistant.components.litejet.const import CONF_DEFAULT_TRANSITION -from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_ON +from homeassistant.const import ( + ATTR_ENTITY_ID, + SERVICE_TURN_OFF, + SERVICE_TURN_ON, + STATE_OFF, + STATE_UNAVAILABLE, +) from homeassistant.core import HomeAssistant from . import async_init_integration @@ -170,3 +176,24 @@ async def test_deactivated_event(hass: HomeAssistant, mock_litejet) -> None: assert not light.is_on(hass, ENTITY_LIGHT) assert hass.states.get(ENTITY_LIGHT).state == "off" assert hass.states.get(ENTITY_OTHER_LIGHT).state == "off" + + +async def test_connected_event(hass, mock_litejet): + """Test handling an event from LiteJet.""" + + await async_init_integration(hass) + + # Initial state is available. + assert hass.states.get(ENTITY_LIGHT).state == STATE_OFF + + # Event indicates it is disconnected now. + mock_litejet.connected_changed(False, "test") + await hass.async_block_till_done() + + assert hass.states.get(ENTITY_LIGHT).state == STATE_UNAVAILABLE + + # Event indicates it is connected now. + mock_litejet.connected_changed(True, None) + await hass.async_block_till_done() + + assert hass.states.get(ENTITY_LIGHT).state == STATE_OFF diff --git a/tests/components/litejet/test_scene.py b/tests/components/litejet/test_scene.py index 43ccc94dcf8..af7a5f10e54 100644 --- a/tests/components/litejet/test_scene.py +++ b/tests/components/litejet/test_scene.py @@ -1,6 +1,11 @@ """The tests for the litejet component.""" from homeassistant.components import scene -from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_ON +from homeassistant.const import ( + ATTR_ENTITY_ID, + SERVICE_TURN_ON, + STATE_UNAVAILABLE, + STATE_UNKNOWN, +) from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er @@ -40,3 +45,24 @@ async def test_activate(hass: HomeAssistant, mock_litejet) -> None: ) mock_litejet.activate_scene.assert_called_once_with(ENTITY_SCENE_NUMBER) + + +async def test_connected_event(hass, mock_litejet): + """Test handling an event from LiteJet.""" + + await async_init_integration(hass, use_scene=True) + + # Initial state is available. + assert hass.states.get(ENTITY_SCENE).state == STATE_UNKNOWN + + # Event indicates it is disconnected now. + mock_litejet.connected_changed(False, "test") + await hass.async_block_till_done() + + assert hass.states.get(ENTITY_SCENE).state == STATE_UNAVAILABLE + + # Event indicates it is connected now. + mock_litejet.connected_changed(True, None) + await hass.async_block_till_done() + + assert hass.states.get(ENTITY_SCENE).state == STATE_UNKNOWN diff --git a/tests/components/litejet/test_switch.py b/tests/components/litejet/test_switch.py index 4592f684b26..21ac42e7e92 100644 --- a/tests/components/litejet/test_switch.py +++ b/tests/components/litejet/test_switch.py @@ -1,6 +1,13 @@ """The tests for the litejet component.""" from homeassistant.components import switch -from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_ON +from homeassistant.const import ( + ATTR_ENTITY_ID, + SERVICE_TURN_OFF, + SERVICE_TURN_ON, + STATE_OFF, + STATE_ON, + STATE_UNAVAILABLE, +) from homeassistant.core import HomeAssistant from . import async_init_integration @@ -16,8 +23,8 @@ async def test_on_off(hass: HomeAssistant, mock_litejet) -> None: await async_init_integration(hass, use_switch=True) - assert hass.states.get(ENTITY_SWITCH).state == "off" - assert hass.states.get(ENTITY_OTHER_SWITCH).state == "off" + assert hass.states.get(ENTITY_SWITCH).state == STATE_OFF + assert hass.states.get(ENTITY_OTHER_SWITCH).state == STATE_OFF assert not switch.is_on(hass, ENTITY_SWITCH) @@ -43,8 +50,8 @@ async def test_pressed_event(hass: HomeAssistant, mock_litejet) -> None: assert switch.is_on(hass, ENTITY_SWITCH) assert not switch.is_on(hass, ENTITY_OTHER_SWITCH) - assert hass.states.get(ENTITY_SWITCH).state == "on" - assert hass.states.get(ENTITY_OTHER_SWITCH).state == "off" + assert hass.states.get(ENTITY_SWITCH).state == STATE_ON + assert hass.states.get(ENTITY_OTHER_SWITCH).state == STATE_OFF # Switch 2 mock_litejet.switch_pressed_callbacks[ENTITY_OTHER_SWITCH_NUMBER]() @@ -52,8 +59,8 @@ async def test_pressed_event(hass: HomeAssistant, mock_litejet) -> None: assert switch.is_on(hass, ENTITY_OTHER_SWITCH) assert switch.is_on(hass, ENTITY_SWITCH) - assert hass.states.get(ENTITY_SWITCH).state == "on" - assert hass.states.get(ENTITY_OTHER_SWITCH).state == "on" + assert hass.states.get(ENTITY_SWITCH).state == STATE_ON + assert hass.states.get(ENTITY_OTHER_SWITCH).state == STATE_ON async def test_released_event(hass: HomeAssistant, mock_litejet) -> None: @@ -73,5 +80,29 @@ async def test_released_event(hass: HomeAssistant, mock_litejet) -> None: assert not switch.is_on(hass, ENTITY_OTHER_SWITCH) assert not switch.is_on(hass, ENTITY_SWITCH) - assert hass.states.get(ENTITY_SWITCH).state == "off" - assert hass.states.get(ENTITY_OTHER_SWITCH).state == "off" + assert hass.states.get(ENTITY_SWITCH).state == STATE_OFF + assert hass.states.get(ENTITY_OTHER_SWITCH).state == STATE_OFF + + +async def test_connected_event(hass, mock_litejet): + """Test handling an event from LiteJet.""" + + await async_init_integration(hass, use_switch=True) + + # Initial state is available. + assert hass.states.get(ENTITY_SWITCH).state == STATE_OFF + assert hass.states.get(ENTITY_OTHER_SWITCH).state == STATE_OFF + + # Event indicates it is disconnected now. + mock_litejet.connected_changed(False, "test") + await hass.async_block_till_done() + + assert hass.states.get(ENTITY_SWITCH).state == STATE_UNAVAILABLE + assert hass.states.get(ENTITY_OTHER_SWITCH).state == STATE_UNAVAILABLE + + # Event indicates it is connected now. + mock_litejet.connected_changed(True, None) + await hass.async_block_till_done() + + assert hass.states.get(ENTITY_SWITCH).state == STATE_OFF + assert hass.states.get(ENTITY_OTHER_SWITCH).state == STATE_OFF