From cbf0560966c76ec377d929b83b0331312278c364 Mon Sep 17 00:00:00 2001 From: Artem Sorokin Date: Fri, 25 Oct 2024 16:38:29 +0300 Subject: [PATCH] Remove deprecated constants (#552) --- .../yandex_smart_home/capability_onoff.py | 7 ++++++- .../yandex_smart_home/capability_range.py | 4 ++-- tests/test_capability_onoff.py | 15 +++++++++++++-- tests/test_capability_range.py | 12 ++++++------ 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/custom_components/yandex_smart_home/capability_onoff.py b/custom_components/yandex_smart_home/capability_onoff.py index bc04f810..c7c00999 100644 --- a/custom_components/yandex_smart_home/capability_onoff.py +++ b/custom_components/yandex_smart_home/capability_onoff.py @@ -28,6 +28,8 @@ from homeassistant.const import ( ATTR_ENTITY_ID, ATTR_SUPPORTED_FEATURES, + MAJOR_VERSION, + MINOR_VERSION, SERVICE_CLOSE_COVER, SERVICE_LOCK, SERVICE_OPEN_COVER, @@ -230,7 +232,10 @@ async def _set_state(self, data: RequestData, state: dict[str, Any]): @register_capability class OnOffCapabilityLock(OnOffCapability): def get_value(self) -> bool: - return self.state.state == lock.STATE_UNLOCKED + if (MAJOR_VERSION == 2024 and MINOR_VERSION >= 10) or MAJOR_VERSION >= 2025: + return self.state.state == lock.LockState.UNLOCKED + else: + return self.state.state == lock.STATE_UNLOCKED def supported(self) -> bool: return self.state.domain == lock.DOMAIN diff --git a/custom_components/yandex_smart_home/capability_range.py b/custom_components/yandex_smart_home/capability_range.py index 47f6befb..5d7fcf77 100644 --- a/custom_components/yandex_smart_home/capability_range.py +++ b/custom_components/yandex_smart_home/capability_range.py @@ -549,7 +549,7 @@ async def set_state(self, data: RequestData, state: dict[str, Any]): media_player.SERVICE_PLAY_MEDIA, { ATTR_ENTITY_ID: self.state.entity_id, media_player.ATTR_MEDIA_CONTENT_ID: int(value), - media_player.ATTR_MEDIA_CONTENT_TYPE: media_player.const.MEDIA_TYPE_CHANNEL + media_player.ATTR_MEDIA_CONTENT_TYPE: media_player.MediaType.CHANNEL }, blocking=False, # some tv's do it too slow context=data.context @@ -566,5 +566,5 @@ async def set_state(self, data: RequestData, state: dict[str, Any]): def _value(self) -> float | None: media_content_type = self.state.attributes.get(media_player.ATTR_MEDIA_CONTENT_TYPE) - if media_content_type == media_player.const.MEDIA_TYPE_CHANNEL: + if media_content_type == media_player.MediaType.CHANNEL: return self._convert_to_float(self.state.attributes.get(media_player.ATTR_MEDIA_CONTENT_ID), strict=False) diff --git a/tests/test_capability_onoff.py b/tests/test_capability_onoff.py index b8b0b09a..325d405f 100644 --- a/tests/test_capability_onoff.py +++ b/tests/test_capability_onoff.py @@ -22,6 +22,8 @@ ATTR_SUPPORTED_FEATURES, CONF_ENTITY_ID, CONF_SERVICE, + MAJOR_VERSION, + MINOR_VERSION, SERVICE_TURN_OFF, SERVICE_TURN_ON, STATE_OFF, @@ -244,7 +246,11 @@ async def test_capability_onoff_media_player(hass): async def test_capability_onoff_lock(hass): - state = State('lock.test', lock.STATE_UNLOCKED) + if (MAJOR_VERSION == 2024 and MINOR_VERSION >= 10) or MAJOR_VERSION >= 2025: + state = State('lock.test', lock.LockState.UNLOCKED) + else: + state = State('lock.test', lock.STATE_UNLOCKED) + cap = get_exact_one_capability(hass, BASIC_CONFIG, state, CAPABILITIES_ONOFF, ON_OFF_INSTANCE_ON) assert cap.retrievable @@ -261,7 +267,12 @@ async def test_capability_onoff_lock(hass): assert len(off_calls) == 1 assert off_calls[0].data == {ATTR_ENTITY_ID: state.entity_id} - for s in [lock.STATE_UNLOCKING, lock.STATE_LOCKING]: + if (MAJOR_VERSION == 2024 and MINOR_VERSION >= 10) or MAJOR_VERSION >= 2025: + states = [lock.LockState.UNLOCKING, lock.LockState.LOCKING] + else: + states = [lock.STATE_UNLOCKING, lock.STATE_LOCKING] + + for s in states: state_other = State('lock.test', s) cap = get_exact_one_capability(hass, BASIC_CONFIG, state_other, CAPABILITIES_ONOFF, ON_OFF_INSTANCE_ON) diff --git a/tests/test_capability_range.py b/tests/test_capability_range.py index 74486774..15773116 100644 --- a/tests/test_capability_range.py +++ b/tests/test_capability_range.py @@ -660,7 +660,7 @@ async def test_capability_range_channel_set_random(hass, caplog): assert calls_set[0].data == { ATTR_ENTITY_ID: state.entity_id, media_player.ATTR_MEDIA_CONTENT_ID: 15, - media_player.ATTR_MEDIA_CONTENT_TYPE: media_player.const.MEDIA_TYPE_CHANNEL + media_player.ATTR_MEDIA_CONTENT_TYPE: media_player.MediaType.CHANNEL } with pytest.raises(SmartHomeError) as e: @@ -693,7 +693,7 @@ async def test_capability_range_channel_set_random_with_value(hass, caplog): ATTR_SUPPORTED_FEATURES: media_player.MediaPlayerEntityFeature.PLAY_MEDIA, ATTR_DEVICE_CLASS: media_player.MediaPlayerDeviceClass.TV, media_player.ATTR_MEDIA_CONTENT_ID: 15, - media_player.ATTR_MEDIA_CONTENT_TYPE: media_player.const.MEDIA_TYPE_CHANNEL + media_player.ATTR_MEDIA_CONTENT_TYPE: media_player.MediaType.CHANNEL }) cap = get_exact_one_capability(hass, BASIC_CONFIG, state, CAPABILITIES_RANGE, RANGE_INSTANCE_CHANNEL) assert cap.retrievable @@ -717,19 +717,19 @@ async def test_capability_range_channel_set_random_with_value(hass, caplog): assert calls_set[0].data == { ATTR_ENTITY_ID: state.entity_id, media_player.ATTR_MEDIA_CONTENT_ID: 20, - media_player.ATTR_MEDIA_CONTENT_TYPE: media_player.const.MEDIA_TYPE_CHANNEL + media_player.ATTR_MEDIA_CONTENT_TYPE: media_player.MediaType.CHANNEL } assert calls_set[1].data == { ATTR_ENTITY_ID: state.entity_id, media_player.ATTR_MEDIA_CONTENT_ID: 12, - media_player.ATTR_MEDIA_CONTENT_TYPE: media_player.const.MEDIA_TYPE_CHANNEL + media_player.ATTR_MEDIA_CONTENT_TYPE: media_player.MediaType.CHANNEL } async def test_capability_range_channel_value(hass, caplog): state = State('media_player.test', STATE_OFF, { ATTR_SUPPORTED_FEATURES: media_player.MediaPlayerEntityFeature.PLAY_MEDIA, - media_player.ATTR_MEDIA_CONTENT_TYPE: media_player.const.MEDIA_TYPE_CHANNEL, + media_player.ATTR_MEDIA_CONTENT_TYPE: media_player.MediaType.CHANNEL, media_player.ATTR_MEDIA_CONTENT_ID: '5' }) cap = get_exact_one_capability(hass, BASIC_CONFIG, state, CAPABILITIES_RANGE, RANGE_INSTANCE_CHANNEL) @@ -745,7 +745,7 @@ async def test_capability_range_channel_value(hass, caplog): state = State('media_player.test', STATE_OFF, { ATTR_SUPPORTED_FEATURES: media_player.MediaPlayerEntityFeature.PLAY_MEDIA, - media_player.ATTR_MEDIA_CONTENT_TYPE: media_player.const.MEDIA_TYPE_CHANNEL, + media_player.ATTR_MEDIA_CONTENT_TYPE: media_player.MediaType.CHANNEL, media_player.ATTR_MEDIA_CONTENT_ID: 'foo' }) cap = get_exact_one_capability(hass, BASIC_CONFIG, state, CAPABILITIES_RANGE, RANGE_INSTANCE_CHANNEL)