From 96409cf0e02c8c71ef66b860636025ea87fea5e9 Mon Sep 17 00:00:00 2001 From: Tobias Sauerwein Date: Sat, 4 Nov 2023 11:28:26 +0100 Subject: [PATCH] Use pyatmo device type enum instead of string (#103030) --- homeassistant/components/netatmo/climate.py | 14 ++++++++++---- homeassistant/components/netatmo/data_handler.py | 7 +++++-- .../components/netatmo/netatmo_entity_base.py | 3 ++- homeassistant/components/netatmo/select.py | 4 +++- homeassistant/components/netatmo/sensor.py | 8 +++++++- 5 files changed, 27 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/netatmo/climate.py b/homeassistant/components/netatmo/climate.py index f471501584448..b0d0439d72c6b 100644 --- a/homeassistant/components/netatmo/climate.py +++ b/homeassistant/components/netatmo/climate.py @@ -5,6 +5,7 @@ from typing import Any, cast from pyatmo.modules import NATherm1 +from pyatmo.modules.device_types import DeviceType import voluptuous as vol from homeassistant.components.climate import ( @@ -106,8 +107,8 @@ DEFAULT_MAX_TEMP = 30 -NA_THERM = "NATherm1" -NA_VALVE = "NRV" +NA_THERM = DeviceType.NATherm1 +NA_VALVE = DeviceType.NRV async def async_setup_entry( @@ -117,6 +118,10 @@ async def async_setup_entry( @callback def _create_entity(netatmo_device: NetatmoRoom) -> None: + if not netatmo_device.room.climate_type: + msg = f"No climate type found for this room: {netatmo_device.room.name}" + _LOGGER.info(msg) + return entity = NetatmoThermostat(netatmo_device) async_add_entities([entity]) @@ -170,7 +175,8 @@ def __init__(self, netatmo_device: NetatmoRoom) -> None: ] ) - self._model: str = f"{self._room.climate_type}" + assert self._room.climate_type + self._model: DeviceType = self._room.climate_type self._config_url = CONF_URL_ENERGY @@ -184,7 +190,7 @@ def __init__(self, netatmo_device: NetatmoRoom) -> None: self._selected_schedule = None self._attr_hvac_modes = [HVACMode.AUTO, HVACMode.HEAT] - if self._model == NA_THERM: + if self._model is NA_THERM: self._attr_hvac_modes.append(HVACMode.OFF) self._attr_unique_id = f"{self._room.entity_id}-{self._model}" diff --git a/homeassistant/components/netatmo/data_handler.py b/homeassistant/components/netatmo/data_handler.py index 8fa8ab2073d2b..e1d100f773e71 100644 --- a/homeassistant/components/netatmo/data_handler.py +++ b/homeassistant/components/netatmo/data_handler.py @@ -12,7 +12,10 @@ import aiohttp import pyatmo -from pyatmo.modules.device_types import DeviceCategory as NetatmoDeviceCategory +from pyatmo.modules.device_types import ( + DeviceCategory as NetatmoDeviceCategory, + DeviceType as NetatmoDeviceType, +) from homeassistant.config_entries import ConfigEntry from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback @@ -53,7 +56,7 @@ HOME = "home" WEATHER = "weather" AIR_CARE = "air_care" -PUBLIC = "public" +PUBLIC = NetatmoDeviceType.public EVENT = "event" PUBLISHERS = { diff --git a/homeassistant/components/netatmo/netatmo_entity_base.py b/homeassistant/components/netatmo/netatmo_entity_base.py index 4cf5766b6b5a0..54915facb3a9c 100644 --- a/homeassistant/components/netatmo/netatmo_entity_base.py +++ b/homeassistant/components/netatmo/netatmo_entity_base.py @@ -3,6 +3,7 @@ from typing import Any +from pyatmo import DeviceType from pyatmo.modules.device_types import ( DEVICE_DESCRIPTION_MAP, DeviceType as NetatmoDeviceType, @@ -29,7 +30,7 @@ def __init__(self, data_handler: NetatmoDataHandler) -> None: self._device_name: str = "" self._id: str = "" - self._model: str = "" + self._model: DeviceType self._config_url: str | None = None self._attr_name = None self._attr_unique_id = None diff --git a/homeassistant/components/netatmo/select.py b/homeassistant/components/netatmo/select.py index 3651ae05e8802..b02c63698f3ba 100644 --- a/homeassistant/components/netatmo/select.py +++ b/homeassistant/components/netatmo/select.py @@ -3,6 +3,8 @@ import logging +from pyatmo import DeviceType + from homeassistant.components.select import SelectEntity from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback @@ -65,7 +67,7 @@ def __init__( self._device_name = self._home.name self._attr_name = f"{self._device_name}" - self._model: str = "NATherm1" + self._model = DeviceType.NATherm1 self._config_url = CONF_URL_ENERGY self._attr_unique_id = f"{self._home_id}-schedule-select" diff --git a/homeassistant/components/netatmo/sensor.py b/homeassistant/components/netatmo/sensor.py index f286e53772c99..fe6b44a0334a1 100644 --- a/homeassistant/components/netatmo/sensor.py +++ b/homeassistant/components/netatmo/sensor.py @@ -322,6 +322,10 @@ def _create_sensor_entity(netatmo_device: NetatmoDevice) -> None: @callback def _create_room_sensor_entity(netatmo_device: NetatmoRoom) -> None: + if not netatmo_device.room.climate_type: + msg = f"No climate type found for this room: {netatmo_device.room.name}" + _LOGGER.info(msg) + return async_add_entities( NetatmoRoomSensor(netatmo_device, description) for description in SENSOR_TYPES @@ -633,9 +637,11 @@ def __init__( self._attr_name = f"{self._room.name} {self.entity_description.name}" self._room_id = self._room.entity_id - self._model = f"{self._room.climate_type}" self._config_url = CONF_URL_ENERGY + assert self._room.climate_type + self._model = self._room.climate_type + self._attr_unique_id = ( f"{self._id}-{self._room.entity_id}-{self.entity_description.key}" )