Skip to content

Commit

Permalink
Remove deprecated constants (close dext0r#425)
Browse files Browse the repository at this point in the history
  • Loading branch information
dext0r committed Feb 8, 2023
1 parent b0bca09 commit 055ccc0
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 119 deletions.
2 changes: 1 addition & 1 deletion custom_components/yandex_smart_home/capability_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ def support_random_access(self) -> bool:
if self.entity_config.get(const.CONF_SUPPORT_SET_CHANNEL) is False:
return False

if device_class == media_player.DEVICE_CLASS_TV:
if device_class == media_player.MediaPlayerDeviceClass.TV:
if features & media_player.MediaPlayerEntityFeature.PLAY_MEDIA or \
const.MEDIA_PLAYER_FEATURE_PLAY_MEDIA in self.entity_config.get(const.CONF_FEATURES, []):
return True
Expand Down
6 changes: 3 additions & 3 deletions custom_components/yandex_smart_home/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@
}

DEVICE_CLASS_TO_YANDEX_TYPES = {
(media_player.DOMAIN, media_player.DEVICE_CLASS_TV): TYPE_MEDIA_DEVICE_TV,
(media_player.DOMAIN, media_player.DEVICE_CLASS_RECEIVER): TYPE_MEDIA_DEVICE_RECIEVER,
(switch.DOMAIN, switch.DEVICE_CLASS_OUTLET): TYPE_SOCKET,
(media_player.DOMAIN, media_player.MediaPlayerDeviceClass.TV): TYPE_MEDIA_DEVICE_TV,
(media_player.DOMAIN, media_player.MediaPlayerDeviceClass.RECEIVER): TYPE_MEDIA_DEVICE_RECIEVER,
(switch.DOMAIN, switch.SwitchDeviceClass.OUTLET): TYPE_SOCKET,
}

ON_OFF_INSTANCE_ON = 'on'
Expand Down
26 changes: 13 additions & 13 deletions custom_components/yandex_smart_home/prop_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ class ContactProperty(EventProperty):
def supported(self) -> bool:
if self.state.domain == binary_sensor.DOMAIN:
return self.state.attributes.get(ATTR_DEVICE_CLASS) in (
binary_sensor.DEVICE_CLASS_DOOR,
binary_sensor.DEVICE_CLASS_GARAGE_DOOR,
binary_sensor.DEVICE_CLASS_WINDOW,
binary_sensor.DEVICE_CLASS_OPENING
binary_sensor.BinarySensorDeviceClass.DOOR,
binary_sensor.BinarySensorDeviceClass.GARAGE_DOOR,
binary_sensor.BinarySensorDeviceClass.WINDOW,
binary_sensor.BinarySensorDeviceClass.OPENING
)

return False
Expand All @@ -166,9 +166,9 @@ class MotionProperty(EventProperty):
def supported(self) -> bool:
if self.state.domain == binary_sensor.DOMAIN:
return self.state.attributes.get(ATTR_DEVICE_CLASS) in (
binary_sensor.DEVICE_CLASS_MOTION,
binary_sensor.DEVICE_CLASS_OCCUPANCY,
binary_sensor.DEVICE_CLASS_PRESENCE
binary_sensor.BinarySensorDeviceClass.MOTION,
binary_sensor.BinarySensorDeviceClass.OCCUPANCY,
binary_sensor.BinarySensorDeviceClass.PRESENCE
)

return False
Expand All @@ -180,7 +180,7 @@ class GasProperty(EventProperty):

def supported(self) -> bool:
if self.state.domain == binary_sensor.DOMAIN:
return self.state.attributes.get(ATTR_DEVICE_CLASS) == binary_sensor.DEVICE_CLASS_GAS
return self.state.attributes.get(ATTR_DEVICE_CLASS) == binary_sensor.BinarySensorDeviceClass.GAS

return False

Expand All @@ -191,7 +191,7 @@ class SmokeProperty(EventProperty):

def supported(self) -> bool:
if self.state.domain == binary_sensor.DOMAIN:
return self.state.attributes.get(ATTR_DEVICE_CLASS) == binary_sensor.DEVICE_CLASS_SMOKE
return self.state.attributes.get(ATTR_DEVICE_CLASS) == binary_sensor.BinarySensorDeviceClass.SMOKE

return False

Expand All @@ -202,7 +202,7 @@ class BatteryLevelLowProperty(EventProperty):

def supported(self) -> bool:
if self.state.domain == binary_sensor.DOMAIN:
return self.state.attributes.get(ATTR_DEVICE_CLASS) == binary_sensor.DEVICE_CLASS_BATTERY
return self.state.attributes.get(ATTR_DEVICE_CLASS) == binary_sensor.BinarySensorDeviceClass.BATTERY

return False

Expand All @@ -224,7 +224,7 @@ class WaterLeakProperty(EventProperty):

def supported(self) -> bool:
if self.state.domain == binary_sensor.DOMAIN:
return self.state.attributes.get(ATTR_DEVICE_CLASS) == binary_sensor.DEVICE_CLASS_MOISTURE
return self.state.attributes.get(ATTR_DEVICE_CLASS) == binary_sensor.BinarySensorDeviceClass.MOISTURE

return False

Expand Down Expand Up @@ -274,7 +274,7 @@ class VibrationBinarySensorProperty(EventProperty):

def supported(self) -> bool:
if self.state.domain == binary_sensor.DOMAIN:
if self.state.attributes.get(ATTR_DEVICE_CLASS) == binary_sensor.DEVICE_CLASS_VIBRATION:
if self.state.attributes.get(ATTR_DEVICE_CLASS) == binary_sensor.BinarySensorDeviceClass.VIBRATION:
return True

return self.state.attributes.get('last_action') in [
Expand All @@ -286,7 +286,7 @@ def supported(self) -> bool:
return False

def get_value(self) -> str | None:
if self.state.attributes.get(ATTR_DEVICE_CLASS) == binary_sensor.DEVICE_CLASS_VIBRATION:
if self.state.attributes.get(ATTR_DEVICE_CLASS) == binary_sensor.BinarySensorDeviceClass.VIBRATION:
return self.event_value(self.state.state)

return self.event_value(self.state.attributes.get('last_action'))
Expand Down
30 changes: 11 additions & 19 deletions custom_components/yandex_smart_home/prop_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import Any

from homeassistant.components import air_quality, climate, fan, humidifier, light, sensor, switch, water_heater
from homeassistant.components.sensor import SensorDeviceClass
from homeassistant.const import (
ATTR_BATTERY_LEVEL,
ATTR_DEVICE_CLASS,
Expand All @@ -16,15 +17,6 @@
CONCENTRATION_MILLIGRAMS_PER_CUBIC_METER,
CONCENTRATION_PARTS_PER_BILLION,
CONCENTRATION_PARTS_PER_MILLION,
DEVICE_CLASS_BATTERY,
DEVICE_CLASS_CO2,
DEVICE_CLASS_CURRENT,
DEVICE_CLASS_HUMIDITY,
DEVICE_CLASS_ILLUMINANCE,
DEVICE_CLASS_POWER,
DEVICE_CLASS_PRESSURE,
DEVICE_CLASS_TEMPERATURE,
DEVICE_CLASS_VOLTAGE,
ELECTRIC_CURRENT_MILLIAMPERE,
PERCENTAGE,
STATE_UNAVAILABLE,
Expand Down Expand Up @@ -165,7 +157,7 @@ class TemperatureProperty(FloatProperty):

def supported(self) -> bool:
if self.state.domain == sensor.DOMAIN:
return self.state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_TEMPERATURE
return self.state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.TEMPERATURE
elif self.state.domain == air_quality.DOMAIN:
return self.state.attributes.get(climate.ATTR_TEMPERATURE) is not None
elif self.state.domain in (climate.DOMAIN, fan.DOMAIN, humidifier.DOMAIN, water_heater.DOMAIN):
Expand All @@ -188,7 +180,7 @@ class HumidityProperty(FloatProperty):

def supported(self) -> bool:
if self.state.domain == sensor.DOMAIN:
return self.state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_HUMIDITY
return self.state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.HUMIDITY
elif self.state.domain == air_quality.DOMAIN:
return self.state.attributes.get(climate.ATTR_HUMIDITY) is not None
elif self.state.domain in (climate.DOMAIN, fan.DOMAIN, humidifier.DOMAIN):
Expand All @@ -211,7 +203,7 @@ class PressureProperty(FloatProperty):

def supported(self) -> bool:
if self.state.domain == sensor.DOMAIN:
if self.state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_PRESSURE:
if self.state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.PRESSURE:
if self.state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) in PRESSURE_TO_PASCAL:
return True

Expand All @@ -237,7 +229,7 @@ class IlluminanceProperty(FloatProperty):

def supported(self) -> bool:
if self.state.domain in (sensor.DOMAIN, light.DOMAIN, fan.DOMAIN):
if self.state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_ILLUMINANCE:
if self.state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.ILLUMINANCE:
return True

return const.ATTR_ILLUMINANCE in self.state.attributes
Expand Down Expand Up @@ -272,7 +264,7 @@ class CO2Property(FloatProperty):

def supported(self) -> bool:
if self.state.domain == sensor.DOMAIN:
return self.state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_CO2
return self.state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.CO2
elif self.state.domain in (air_quality.DOMAIN, fan.DOMAIN):
return air_quality.ATTR_CO2 in self.state.attributes

Expand Down Expand Up @@ -350,7 +342,7 @@ class VoltageProperty(FloatProperty):

def supported(self) -> bool:
if self.state.domain == sensor.DOMAIN:
return self.state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_VOLTAGE
return self.state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.VOLTAGE
elif self.state.domain in (switch.DOMAIN, light.DOMAIN):
return ATTR_VOLTAGE in self.state.attributes

Expand All @@ -369,7 +361,7 @@ class CurrentProperty(FloatProperty):

def supported(self) -> bool:
if self.state.domain == sensor.DOMAIN:
return self.state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_CURRENT
return self.state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.CURRENT
elif self.state.domain in (switch.DOMAIN, light.DOMAIN):
return const.ATTR_CURRENT in self.state.attributes

Expand All @@ -390,7 +382,7 @@ class PowerProperty(FloatProperty):

def supported(self) -> bool:
if self.state.domain == sensor.DOMAIN:
return self.state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_POWER
return self.state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.POWER
elif self.state.domain == switch.DOMAIN:
for attribute in [const.ATTR_POWER, const.ATTR_LOAD_POWER, const.ATTR_CURRENT_CONSUMPTION]:
if attribute in self.state.attributes:
Expand All @@ -415,15 +407,15 @@ class BatteryLevelProperty(FloatProperty):
instance = const.FLOAT_INSTANCE_BATTERY_LEVEL

def supported(self) -> bool:
if self.state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_BATTERY and \
if self.state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.BATTERY and \
self.state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PERCENTAGE:
return True

return ATTR_BATTERY_LEVEL in self.state.attributes

def get_value(self) -> float | None:
value = None
if self.state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_BATTERY:
if self.state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.BATTERY:
value = self.state.state
elif ATTR_BATTERY_LEVEL in self.state.attributes:
value = self.state.attributes.get(ATTR_BATTERY_LEVEL)
Expand Down
16 changes: 8 additions & 8 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from homeassistant.components.demo.binary_sensor import DemoBinarySensor
from homeassistant.components.demo.light import DemoLight
from homeassistant.components.demo.sensor import DemoSensor
from homeassistant.components.sensor import STATE_CLASS_MEASUREMENT
from homeassistant.const import DEVICE_CLASS_TEMPERATURE, TEMP_CELSIUS
from homeassistant.components.sensor import SensorDeviceClass, SensorStateClass
from homeassistant.const import UnitOfTemperature
from homeassistant.helpers import entityfilter
from homeassistant.setup import async_setup_component
import pytest
Expand Down Expand Up @@ -68,9 +68,9 @@ def hass_platform(event_loop: asyncio.AbstractEventLoop, hass, config_entry):
unique_id='outside_temp',
name='Outside Temperature',
state=15.6,
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
unit_of_measurement=TEMP_CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
unit_of_measurement=UnitOfTemperature.CELSIUS,
battery=None
)
demo_sensor.hass = hass
Expand Down Expand Up @@ -113,9 +113,9 @@ def hass_platform_cloud_connection(event_loop: asyncio.AbstractEventLoop, hass,
unique_id='outside_temp',
name='Outside Temperature',
state=15.6,
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
unit_of_measurement=TEMP_CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
unit_of_measurement=UnitOfTemperature.CELSIUS,
battery=None
)
demo_sensor.hass = hass
Expand Down
18 changes: 10 additions & 8 deletions tests/test_capability_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ async def test_capability_range_channel_via_features(hass):
async def test_capability_range_channel_set_via_config(hass):
state = State('media_player.test', STATE_OFF, {
ATTR_SUPPORTED_FEATURES: media_player.MediaPlayerEntityFeature.PLAY_MEDIA,
ATTR_DEVICE_CLASS: media_player.DEVICE_CLASS_TV
ATTR_DEVICE_CLASS: media_player.MediaPlayerDeviceClass.TV
})
config = MockConfig(
entity_config={
Expand All @@ -604,7 +604,7 @@ async def test_capability_range_channel_set_via_config(hass):
media_player.MediaPlayerEntityFeature.PLAY_MEDIA |
media_player.MediaPlayerEntityFeature.PREVIOUS_TRACK |
media_player.MediaPlayerEntityFeature.NEXT_TRACK,
ATTR_DEVICE_CLASS: media_player.DEVICE_CLASS_TV
ATTR_DEVICE_CLASS: media_player.MediaPlayerDeviceClass.TV
})

cap = get_exact_one_capability(hass, config, state, CAPABILITIES_RANGE, RANGE_INSTANCE_CHANNEL)
Expand All @@ -622,15 +622,15 @@ async def test_capability_range_channel_set_random(hass, caplog):

state = State('media_player.test', STATE_OFF, {
ATTR_SUPPORTED_FEATURES: media_player.MediaPlayerEntityFeature.PLAY_MEDIA,
ATTR_DEVICE_CLASS: media_player.DEVICE_CLASS_RECEIVER
ATTR_DEVICE_CLASS: media_player.MediaPlayerDeviceClass.RECEIVER
})
cap = get_exact_one_capability(hass, BASIC_CONFIG, state, CAPABILITIES_RANGE, RANGE_INSTANCE_CHANNEL)
assert cap.retrievable is False
assert cap.support_random_access is False

state = State('media_player.test', STATE_OFF, {
ATTR_SUPPORTED_FEATURES: media_player.MediaPlayerEntityFeature.PLAY_MEDIA,
ATTR_DEVICE_CLASS: media_player.DEVICE_CLASS_TV
ATTR_DEVICE_CLASS: media_player.MediaPlayerDeviceClass.TV
})
cap = get_exact_one_capability(hass, BASIC_CONFIG, state, CAPABILITIES_RANGE, RANGE_INSTANCE_CHANNEL)
assert cap.retrievable
Expand Down Expand Up @@ -668,7 +668,7 @@ async def test_capability_range_channel_set_random(hass, caplog):
async def test_capability_range_channel_set_not_supported(hass, caplog):
state = State('media_player.test', STATE_OFF, {
ATTR_SUPPORTED_FEATURES: media_player.MediaPlayerEntityFeature.PLAY_MEDIA,
ATTR_DEVICE_CLASS: media_player.DEVICE_CLASS_TV
ATTR_DEVICE_CLASS: media_player.MediaPlayerDeviceClass.TV
})
cap = get_exact_one_capability(hass, BASIC_CONFIG, state, CAPABILITIES_RANGE, RANGE_INSTANCE_CHANNEL)
assert cap.retrievable is True
Expand All @@ -684,7 +684,7 @@ async def test_capability_range_channel_set_not_supported(hass, caplog):
async def test_capability_range_channel_set_random_with_value(hass, caplog):
state = State('media_player.test', STATE_OFF, {
ATTR_SUPPORTED_FEATURES: media_player.MediaPlayerEntityFeature.PLAY_MEDIA,
ATTR_DEVICE_CLASS: media_player.DEVICE_CLASS_TV,
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
})
Expand Down Expand Up @@ -752,7 +752,9 @@ async def test_capability_range_channel_value(hass, caplog):
media_player.MediaPlayerEntityFeature.NEXT_TRACK |
media_player.MediaPlayerEntityFeature.PLAY_MEDIA
])
@pytest.mark.parametrize('device_class', [media_player.DEVICE_CLASS_TV, media_player.DEVICE_CLASS_RECEIVER])
@pytest.mark.parametrize('device_class', [
media_player.MediaPlayerDeviceClass.TV, media_player.MediaPlayerDeviceClass.RECEIVER
])
async def test_capability_range_channel_set_relative(hass, features, device_class):
state = State('media_player.test', STATE_OFF, {
ATTR_SUPPORTED_FEATURES: media_player.MediaPlayerEntityFeature.PREVIOUS_TRACK
Expand All @@ -764,7 +766,7 @@ async def test_capability_range_channel_set_relative(hass, features, device_clas
ATTR_DEVICE_CLASS: device_class
})
cap = get_exact_one_capability(hass, BASIC_CONFIG, state, CAPABILITIES_RANGE, RANGE_INSTANCE_CHANNEL)
if device_class == media_player.DEVICE_CLASS_TV:
if device_class == media_player.MediaPlayerDeviceClass.TV:
assert cap.retrievable is bool(features & media_player.MediaPlayerEntityFeature.PLAY_MEDIA)
assert cap.support_random_access is bool(features & media_player.MediaPlayerEntityFeature.PLAY_MEDIA)
else:
Expand Down
Loading

0 comments on commit 055ccc0

Please sign in to comment.