Use entity class attributes for Bsblan (#53165)

This commit is contained in:
Robert Hillis 2021-07-21 13:37:12 -04:00 committed by GitHub
parent db1a8e9336
commit a1df3519db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 90 deletions

View File

@ -132,9 +132,7 @@ omit =
homeassistant/components/brottsplatskartan/sensor.py
homeassistant/components/browser/*
homeassistant/components/brunt/cover.py
homeassistant/components/bsblan/__init__.py
homeassistant/components/bsblan/climate.py
homeassistant/components/bsblan/const.py
homeassistant/components/bt_home_hub_5/device_tracker.py
homeassistant/components/bt_smarthub/device_tracker.py
homeassistant/components/buienradar/sensor.py

View File

@ -27,7 +27,6 @@ from homeassistant.const import (
TEMP_FAHRENHEIT,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import (
@ -88,6 +87,10 @@ async def async_setup_entry(
class BSBLanClimate(ClimateEntity):
"""Defines a BSBLan climate device."""
_attr_supported_features = SUPPORT_FLAGS
_attr_hvac_modes = HVAC_MODES
_attr_preset_modes = PRESET_MODES
def __init__(
self,
entry_id: str,
@ -95,89 +98,33 @@ class BSBLanClimate(ClimateEntity):
info: Info,
) -> None:
"""Initialize BSBLan climate device."""
self._current_temperature: float | None = None
self._available = True
self._hvac_mode: str | None = None
self._target_temperature: float | None = None
self._temperature_unit = None
self._preset_mode: str | None = None
self._attr_available = True
self._store_hvac_mode = None
self._info: Info = info
self.bsblan = bsblan
@property
def name(self) -> str:
"""Return the name of the entity."""
return self._info.device_identification
@property
def available(self) -> bool:
"""Return True if entity is available."""
return self._available
@property
def unique_id(self) -> str:
"""Return the unique ID for this sensor."""
return self._info.device_identification
@property
def temperature_unit(self) -> str:
"""Return the unit of measurement which this thermostat uses."""
if self._temperature_unit == "°C":
return TEMP_CELSIUS
return TEMP_FAHRENHEIT
@property
def supported_features(self) -> int:
"""Flag supported features."""
return SUPPORT_FLAGS
@property
def current_temperature(self):
"""Return the current temperature."""
return self._current_temperature
@property
def hvac_mode(self):
"""Return the current operation mode."""
return self._hvac_mode
@property
def hvac_modes(self):
"""Return the list of available operation modes."""
return HVAC_MODES
@property
def target_temperature(self):
"""Return the temperature we try to reach."""
return self._target_temperature
@property
def preset_modes(self):
"""List of available preset modes."""
return PRESET_MODES
@property
def preset_mode(self):
"""Return the preset_mode."""
return self._preset_mode
self._attr_name = self._attr_unique_id = info.device_identification
self._attr_device_info = {
ATTR_IDENTIFIERS: {(DOMAIN, info.device_identification)},
ATTR_NAME: "BSBLan Device",
ATTR_MANUFACTURER: "BSBLan",
ATTR_MODEL: info.controller_variant,
}
async def async_set_preset_mode(self, preset_mode):
"""Set preset mode."""
_LOGGER.debug("Setting preset mode to: %s", preset_mode)
if preset_mode == PRESET_NONE:
# restore previous hvac mode
self._hvac_mode = self._store_hvac_mode
self._attr_hvac_mode = self._store_hvac_mode
else:
# Store hvac mode.
self._store_hvac_mode = self._hvac_mode
self._store_hvac_mode = self._attr_hvac_mode
await self.async_set_data(preset_mode=preset_mode)
async def async_set_hvac_mode(self, hvac_mode):
"""Set HVAC mode."""
_LOGGER.debug("Setting HVAC mode to: %s", hvac_mode)
# preset should be none when hvac mode is set
self._preset_mode = PRESET_NONE
self._attr_preset_mode = PRESET_NONE
await self.async_set_data(hvac_mode=hvac_mode)
async def async_set_temperature(self, **kwargs):
@ -204,39 +151,33 @@ class BSBLanClimate(ClimateEntity):
await self.bsblan.thermostat(**data)
except BSBLanError:
_LOGGER.error("An error occurred while updating the BSBLan device")
self._available = False
self._attr_available = False
async def async_update(self) -> None:
"""Update BSBlan entity."""
try:
state: State = await self.bsblan.state()
except BSBLanError:
if self._available:
if self.available:
_LOGGER.error("An error occurred while updating the BSBLan device")
self._available = False
self._attr_available = False
return
self._available = True
self._attr_available = True
self._current_temperature = float(state.current_temperature.value)
self._target_temperature = float(state.target_temperature.value)
self._attr_current_temperature = float(state.current_temperature.value)
self._attr_target_temperature = float(state.target_temperature.value)
# check if preset is active else get hvac mode
_LOGGER.debug("state hvac/preset mode: %s", state.hvac_mode.value)
if state.hvac_mode.value == "2":
self._preset_mode = PRESET_ECO
self._attr_preset_mode = PRESET_ECO
else:
self._hvac_mode = BSBLAN_TO_HA_STATE[state.hvac_mode.value]
self._preset_mode = PRESET_NONE
self._attr_hvac_mode = BSBLAN_TO_HA_STATE[state.hvac_mode.value]
self._attr_preset_mode = PRESET_NONE
self._temperature_unit = state.current_temperature.unit
@property
def device_info(self) -> DeviceInfo:
"""Return device information about this BSBLan device."""
return {
ATTR_IDENTIFIERS: {(DOMAIN, self._info.device_identification)},
ATTR_NAME: "BSBLan Device",
ATTR_MANUFACTURER: "BSBLan",
ATTR_MODEL: self._info.controller_variant,
}
self._attr_temperature_unit = (
TEMP_CELSIUS
if state.current_temperature.unit == "°C"
else TEMP_FAHRENHEIT
)