From 890eed11211c496aff9f8b038203b9d9a573954b Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Mon, 4 Sep 2023 13:18:55 +0200 Subject: [PATCH] Use shorthand attributes in LCN (#99587) --- homeassistant/components/lcn/binary_sensor.py | 26 +---- homeassistant/components/lcn/cover.py | 108 ++++++------------ homeassistant/components/lcn/light.py | 40 ++----- homeassistant/components/lcn/sensor.py | 30 ++--- homeassistant/components/lcn/switch.py | 30 ++--- 5 files changed, 70 insertions(+), 164 deletions(-) diff --git a/homeassistant/components/lcn/binary_sensor.py b/homeassistant/components/lcn/binary_sensor.py index 13a2a5b3bb3..ceeeecf50c4 100644 --- a/homeassistant/components/lcn/binary_sensor.py +++ b/homeassistant/components/lcn/binary_sensor.py @@ -66,8 +66,6 @@ class LcnRegulatorLockSensor(LcnEntity, BinarySensorEntity): config[CONF_DOMAIN_DATA][CONF_SOURCE] ] - self._value = None - async def async_added_to_hass(self) -> None: """Run when entity about to be added to hass.""" await super().async_added_to_hass() @@ -84,11 +82,6 @@ class LcnRegulatorLockSensor(LcnEntity, BinarySensorEntity): self.setpoint_variable ) - @property - def is_on(self) -> bool | None: - """Return true if the binary sensor is on.""" - return self._value - def input_received(self, input_obj: InputType) -> None: """Set sensor value when LCN input object (command) is received.""" if ( @@ -97,7 +90,7 @@ class LcnRegulatorLockSensor(LcnEntity, BinarySensorEntity): ): return - self._value = input_obj.get_value().is_locked_regulator() + self._attr_is_on = input_obj.get_value().is_locked_regulator() self.async_write_ha_state() @@ -114,8 +107,6 @@ class LcnBinarySensor(LcnEntity, BinarySensorEntity): config[CONF_DOMAIN_DATA][CONF_SOURCE] ] - self._value = None - async def async_added_to_hass(self) -> None: """Run when entity about to be added to hass.""" await super().async_added_to_hass() @@ -132,17 +123,12 @@ class LcnBinarySensor(LcnEntity, BinarySensorEntity): self.bin_sensor_port ) - @property - def is_on(self) -> bool | None: - """Return true if the binary sensor is on.""" - return self._value - def input_received(self, input_obj: InputType) -> None: """Set sensor value when LCN input object (command) is received.""" if not isinstance(input_obj, pypck.inputs.ModStatusBinSensors): return - self._value = input_obj.get_state(self.bin_sensor_port.value) + self._attr_is_on = input_obj.get_state(self.bin_sensor_port.value) self.async_write_ha_state() @@ -156,7 +142,6 @@ class LcnLockKeysSensor(LcnEntity, BinarySensorEntity): super().__init__(config, entry_id, device_connection) self.source = pypck.lcn_defs.Key[config[CONF_DOMAIN_DATA][CONF_SOURCE]] - self._value = None async def async_added_to_hass(self) -> None: """Run when entity about to be added to hass.""" @@ -170,11 +155,6 @@ class LcnLockKeysSensor(LcnEntity, BinarySensorEntity): if not self.device_connection.is_group: await self.device_connection.cancel_status_request_handler(self.source) - @property - def is_on(self) -> bool | None: - """Return true if the binary sensor is on.""" - return self._value - def input_received(self, input_obj: InputType) -> None: """Set sensor value when LCN input object (command) is received.""" if ( @@ -186,5 +166,5 @@ class LcnLockKeysSensor(LcnEntity, BinarySensorEntity): table_id = ord(self.source.name[0]) - 65 key_id = int(self.source.name[1]) - 1 - self._value = input_obj.get_state(table_id, key_id) + self._attr_is_on = input_obj.get_state(table_id, key_id) self.async_write_ha_state() diff --git a/homeassistant/components/lcn/cover.py b/homeassistant/components/lcn/cover.py index bc83da55888..31b2dbface0 100644 --- a/homeassistant/components/lcn/cover.py +++ b/homeassistant/components/lcn/cover.py @@ -51,6 +51,11 @@ async def async_setup_entry( class LcnOutputsCover(LcnEntity, CoverEntity): """Representation of a LCN cover connected to output ports.""" + _attr_is_closed = False + _attr_is_closing = False + _attr_is_opening = False + _attr_assumed_state = True + def __init__( self, config: ConfigType, entry_id: str, device_connection: DeviceConnectionType ) -> None: @@ -68,10 +73,6 @@ class LcnOutputsCover(LcnEntity, CoverEntity): else: self.reverse_time = None - self._is_closed = False - self._is_closing = False - self._is_opening = False - async def async_added_to_hass(self) -> None: """Run when entity about to be added to hass.""" await super().async_added_to_hass() @@ -94,26 +95,6 @@ class LcnOutputsCover(LcnEntity, CoverEntity): pypck.lcn_defs.OutputPort["OUTPUTDOWN"] ) - @property - def is_closed(self) -> bool: - """Return if the cover is closed.""" - return self._is_closed - - @property - def is_opening(self) -> bool: - """Return if the cover is opening or not.""" - return self._is_opening - - @property - def is_closing(self) -> bool: - """Return if the cover is closing or not.""" - return self._is_closing - - @property - def assumed_state(self) -> bool: - """Return True if unable to access real state of the entity.""" - return True - async def async_close_cover(self, **kwargs: Any) -> None: """Close the cover.""" state = pypck.lcn_defs.MotorStateModifier.DOWN @@ -121,8 +102,8 @@ class LcnOutputsCover(LcnEntity, CoverEntity): state, self.reverse_time ): return - self._is_opening = False - self._is_closing = True + self._attr_is_opening = False + self._attr_is_closing = True self.async_write_ha_state() async def async_open_cover(self, **kwargs: Any) -> None: @@ -132,9 +113,9 @@ class LcnOutputsCover(LcnEntity, CoverEntity): state, self.reverse_time ): return - self._is_closed = False - self._is_opening = True - self._is_closing = False + self._attr_is_closed = False + self._attr_is_opening = True + self._attr_is_closing = False self.async_write_ha_state() async def async_stop_cover(self, **kwargs: Any) -> None: @@ -142,8 +123,8 @@ class LcnOutputsCover(LcnEntity, CoverEntity): state = pypck.lcn_defs.MotorStateModifier.STOP if not await self.device_connection.control_motors_outputs(state): return - self._is_closing = False - self._is_opening = False + self._attr_is_closing = False + self._attr_is_opening = False self.async_write_ha_state() def input_received(self, input_obj: InputType) -> None: @@ -156,17 +137,17 @@ class LcnOutputsCover(LcnEntity, CoverEntity): if input_obj.get_percent() > 0: # motor is on if input_obj.get_output_id() == self.output_ids[0]: - self._is_opening = True - self._is_closing = False + self._attr_is_opening = True + self._attr_is_closing = False else: # self.output_ids[1] - self._is_opening = False - self._is_closing = True - self._is_closed = self._is_closing + self._attr_is_opening = False + self._attr_is_closing = True + self._attr_is_closed = self._attr_is_closing else: # motor is off # cover is assumed to be closed if we were in closing state before - self._is_closed = self._is_closing - self._is_closing = False - self._is_opening = False + self._attr_is_closed = self._attr_is_closing + self._attr_is_closing = False + self._attr_is_opening = False self.async_write_ha_state() @@ -174,6 +155,11 @@ class LcnOutputsCover(LcnEntity, CoverEntity): class LcnRelayCover(LcnEntity, CoverEntity): """Representation of a LCN cover connected to relays.""" + _attr_is_closed = False + _attr_is_closing = False + _attr_is_opening = False + _attr_assumed_state = True + def __init__( self, config: ConfigType, entry_id: str, device_connection: DeviceConnectionType ) -> None: @@ -200,34 +186,14 @@ class LcnRelayCover(LcnEntity, CoverEntity): if not self.device_connection.is_group: await self.device_connection.cancel_status_request_handler(self.motor) - @property - def is_closed(self) -> bool: - """Return if the cover is closed.""" - return self._is_closed - - @property - def is_opening(self) -> bool: - """Return if the cover is opening or not.""" - return self._is_opening - - @property - def is_closing(self) -> bool: - """Return if the cover is closing or not.""" - return self._is_closing - - @property - def assumed_state(self) -> bool: - """Return True if unable to access real state of the entity.""" - return True - async def async_close_cover(self, **kwargs: Any) -> None: """Close the cover.""" states = [pypck.lcn_defs.MotorStateModifier.NOCHANGE] * 4 states[self.motor.value] = pypck.lcn_defs.MotorStateModifier.DOWN if not await self.device_connection.control_motors_relays(states): return - self._is_opening = False - self._is_closing = True + self._attr_is_opening = False + self._attr_is_closing = True self.async_write_ha_state() async def async_open_cover(self, **kwargs: Any) -> None: @@ -236,9 +202,9 @@ class LcnRelayCover(LcnEntity, CoverEntity): states[self.motor.value] = pypck.lcn_defs.MotorStateModifier.UP if not await self.device_connection.control_motors_relays(states): return - self._is_closed = False - self._is_opening = True - self._is_closing = False + self._attr_is_closed = False + self._attr_is_opening = True + self._attr_is_closing = False self.async_write_ha_state() async def async_stop_cover(self, **kwargs: Any) -> None: @@ -247,8 +213,8 @@ class LcnRelayCover(LcnEntity, CoverEntity): states[self.motor.value] = pypck.lcn_defs.MotorStateModifier.STOP if not await self.device_connection.control_motors_relays(states): return - self._is_closing = False - self._is_opening = False + self._attr_is_closing = False + self._attr_is_opening = False self.async_write_ha_state() def input_received(self, input_obj: InputType) -> None: @@ -258,11 +224,11 @@ class LcnRelayCover(LcnEntity, CoverEntity): states = input_obj.states # list of boolean values (relay on/off) if states[self.motor_port_onoff]: # motor is on - self._is_opening = not states[self.motor_port_updown] # set direction - self._is_closing = states[self.motor_port_updown] # set direction + self._attr_is_opening = not states[self.motor_port_updown] # set direction + self._attr_is_closing = states[self.motor_port_updown] # set direction else: # motor is off - self._is_opening = False - self._is_closing = False - self._is_closed = states[self.motor_port_updown] + self._attr_is_opening = False + self._attr_is_closing = False + self._attr_is_closed = states[self.motor_port_updown] self.async_write_ha_state() diff --git a/homeassistant/components/lcn/light.py b/homeassistant/components/lcn/light.py index 38480cc3124..65c1344edf0 100644 --- a/homeassistant/components/lcn/light.py +++ b/homeassistant/components/lcn/light.py @@ -65,6 +65,8 @@ class LcnOutputLight(LcnEntity, LightEntity): """Representation of a LCN light for output ports.""" _attr_supported_features = LightEntityFeature.TRANSITION + _attr_is_on = False + _attr_brightness = 255 def __init__( self, config: ConfigType, entry_id: str, device_connection: DeviceConnectionType @@ -79,8 +81,6 @@ class LcnOutputLight(LcnEntity, LightEntity): ) self.dimmable = config[CONF_DOMAIN_DATA][CONF_DIMMABLE] - self._brightness = 255 - self._is_on = False self._is_dimming_to_zero = False if self.dimmable: @@ -101,16 +101,6 @@ class LcnOutputLight(LcnEntity, LightEntity): if not self.device_connection.is_group: await self.device_connection.cancel_status_request_handler(self.output) - @property - def brightness(self) -> int | None: - """Return the brightness of this light between 0..255.""" - return self._brightness - - @property - def is_on(self) -> bool: - """Return True if entity is on.""" - return self._is_on - async def async_turn_on(self, **kwargs: Any) -> None: """Turn the entity on.""" if ATTR_BRIGHTNESS in kwargs: @@ -128,7 +118,7 @@ class LcnOutputLight(LcnEntity, LightEntity): self.output.value, percent, transition ): return - self._is_on = True + self._attr_is_on = True self._is_dimming_to_zero = False self.async_write_ha_state() @@ -146,7 +136,7 @@ class LcnOutputLight(LcnEntity, LightEntity): ): return self._is_dimming_to_zero = bool(transition) - self._is_on = False + self._attr_is_on = False self.async_write_ha_state() def input_received(self, input_obj: InputType) -> None: @@ -157,11 +147,11 @@ class LcnOutputLight(LcnEntity, LightEntity): ): return - self._brightness = int(input_obj.get_percent() / 100.0 * 255) - if self.brightness == 0: + self._attr_brightness = int(input_obj.get_percent() / 100.0 * 255) + if self._attr_brightness == 0: self._is_dimming_to_zero = False - if not self._is_dimming_to_zero and self.brightness is not None: - self._is_on = self.brightness > 0 + if not self._is_dimming_to_zero and self._attr_brightness is not None: + self._attr_is_on = self._attr_brightness > 0 self.async_write_ha_state() @@ -170,6 +160,7 @@ class LcnRelayLight(LcnEntity, LightEntity): _attr_color_mode = ColorMode.ONOFF _attr_supported_color_modes = {ColorMode.ONOFF} + _attr_is_on = False def __init__( self, config: ConfigType, entry_id: str, device_connection: DeviceConnectionType @@ -179,8 +170,6 @@ class LcnRelayLight(LcnEntity, LightEntity): self.output = pypck.lcn_defs.RelayPort[config[CONF_DOMAIN_DATA][CONF_OUTPUT]] - self._is_on = False - async def async_added_to_hass(self) -> None: """Run when entity about to be added to hass.""" await super().async_added_to_hass() @@ -193,18 +182,13 @@ class LcnRelayLight(LcnEntity, LightEntity): if not self.device_connection.is_group: await self.device_connection.cancel_status_request_handler(self.output) - @property - def is_on(self) -> bool: - """Return True if entity is on.""" - return self._is_on - async def async_turn_on(self, **kwargs: Any) -> None: """Turn the entity on.""" states = [pypck.lcn_defs.RelayStateModifier.NOCHANGE] * 8 states[self.output.value] = pypck.lcn_defs.RelayStateModifier.ON if not await self.device_connection.control_relays(states): return - self._is_on = True + self._attr_is_on = True self.async_write_ha_state() async def async_turn_off(self, **kwargs: Any) -> None: @@ -213,7 +197,7 @@ class LcnRelayLight(LcnEntity, LightEntity): states[self.output.value] = pypck.lcn_defs.RelayStateModifier.OFF if not await self.device_connection.control_relays(states): return - self._is_on = False + self._attr_is_on = False self.async_write_ha_state() def input_received(self, input_obj: InputType) -> None: @@ -221,5 +205,5 @@ class LcnRelayLight(LcnEntity, LightEntity): if not isinstance(input_obj, pypck.inputs.ModStatusRelays): return - self._is_on = input_obj.get_state(self.output.value) + self._attr_is_on = input_obj.get_state(self.output.value) self.async_write_ha_state() diff --git a/homeassistant/components/lcn/sensor.py b/homeassistant/components/lcn/sensor.py index 66321c79a1b..1428019b59f 100644 --- a/homeassistant/components/lcn/sensor.py +++ b/homeassistant/components/lcn/sensor.py @@ -77,8 +77,7 @@ class LcnVariableSensor(LcnEntity, SensorEntity): self.unit = pypck.lcn_defs.VarUnit.parse( config[CONF_DOMAIN_DATA][CONF_UNIT_OF_MEASUREMENT] ) - - self._value = None + self._attr_native_unit_of_measurement = cast(str, self.unit.value) async def async_added_to_hass(self) -> None: """Run when entity about to be added to hass.""" @@ -92,16 +91,6 @@ class LcnVariableSensor(LcnEntity, SensorEntity): if not self.device_connection.is_group: await self.device_connection.cancel_status_request_handler(self.variable) - @property - def native_value(self) -> str | None: - """Return the state of the entity.""" - return self._value - - @property - def native_unit_of_measurement(self) -> str: - """Return the unit of measurement of this entity, if any.""" - return cast(str, self.unit.value) - def input_received(self, input_obj: InputType) -> None: """Set sensor value when LCN input object (command) is received.""" if ( @@ -110,7 +99,7 @@ class LcnVariableSensor(LcnEntity, SensorEntity): ): return - self._value = input_obj.get_value().to_var_unit(self.unit) + self._attr_native_value = input_obj.get_value().to_var_unit(self.unit) self.async_write_ha_state() @@ -130,8 +119,6 @@ class LcnLedLogicSensor(LcnEntity, SensorEntity): config[CONF_DOMAIN_DATA][CONF_SOURCE] ] - self._value = None - async def async_added_to_hass(self) -> None: """Run when entity about to be added to hass.""" await super().async_added_to_hass() @@ -144,19 +131,18 @@ class LcnLedLogicSensor(LcnEntity, SensorEntity): if not self.device_connection.is_group: await self.device_connection.cancel_status_request_handler(self.source) - @property - def native_value(self) -> str | None: - """Return the state of the entity.""" - return self._value - def input_received(self, input_obj: InputType) -> None: """Set sensor value when LCN input object (command) is received.""" if not isinstance(input_obj, pypck.inputs.ModStatusLedsAndLogicOps): return if self.source in pypck.lcn_defs.LedPort: - self._value = input_obj.get_led_state(self.source.value).name.lower() + self._attr_native_value = input_obj.get_led_state( + self.source.value + ).name.lower() elif self.source in pypck.lcn_defs.LogicOpPort: - self._value = input_obj.get_logic_op_state(self.source.value).name.lower() + self._attr_native_value = input_obj.get_logic_op_state( + self.source.value + ).name.lower() self.async_write_ha_state() diff --git a/homeassistant/components/lcn/switch.py b/homeassistant/components/lcn/switch.py index ded15c0f1da..8374ff85ab7 100644 --- a/homeassistant/components/lcn/switch.py +++ b/homeassistant/components/lcn/switch.py @@ -52,6 +52,8 @@ async def async_setup_entry( class LcnOutputSwitch(LcnEntity, SwitchEntity): """Representation of a LCN switch for output ports.""" + _attr_is_on = False + def __init__( self, config: ConfigType, entry_id: str, device_connection: DeviceConnectionType ) -> None: @@ -60,8 +62,6 @@ class LcnOutputSwitch(LcnEntity, SwitchEntity): self.output = pypck.lcn_defs.OutputPort[config[CONF_DOMAIN_DATA][CONF_OUTPUT]] - self._is_on = False - async def async_added_to_hass(self) -> None: """Run when entity about to be added to hass.""" await super().async_added_to_hass() @@ -74,23 +74,18 @@ class LcnOutputSwitch(LcnEntity, SwitchEntity): if not self.device_connection.is_group: await self.device_connection.cancel_status_request_handler(self.output) - @property - def is_on(self) -> bool: - """Return True if entity is on.""" - return self._is_on - async def async_turn_on(self, **kwargs: Any) -> None: """Turn the entity on.""" if not await self.device_connection.dim_output(self.output.value, 100, 0): return - self._is_on = True + self._attr_is_on = True self.async_write_ha_state() async def async_turn_off(self, **kwargs: Any) -> None: """Turn the entity off.""" if not await self.device_connection.dim_output(self.output.value, 0, 0): return - self._is_on = False + self._attr_is_on = False self.async_write_ha_state() def input_received(self, input_obj: InputType) -> None: @@ -101,13 +96,15 @@ class LcnOutputSwitch(LcnEntity, SwitchEntity): ): return - self._is_on = input_obj.get_percent() > 0 + self._attr_is_on = input_obj.get_percent() > 0 self.async_write_ha_state() class LcnRelaySwitch(LcnEntity, SwitchEntity): """Representation of a LCN switch for relay ports.""" + _attr_is_on = False + def __init__( self, config: ConfigType, entry_id: str, device_connection: DeviceConnectionType ) -> None: @@ -116,8 +113,6 @@ class LcnRelaySwitch(LcnEntity, SwitchEntity): self.output = pypck.lcn_defs.RelayPort[config[CONF_DOMAIN_DATA][CONF_OUTPUT]] - self._is_on = False - async def async_added_to_hass(self) -> None: """Run when entity about to be added to hass.""" await super().async_added_to_hass() @@ -130,18 +125,13 @@ class LcnRelaySwitch(LcnEntity, SwitchEntity): if not self.device_connection.is_group: await self.device_connection.cancel_status_request_handler(self.output) - @property - def is_on(self) -> bool: - """Return True if entity is on.""" - return self._is_on - async def async_turn_on(self, **kwargs: Any) -> None: """Turn the entity on.""" states = [pypck.lcn_defs.RelayStateModifier.NOCHANGE] * 8 states[self.output.value] = pypck.lcn_defs.RelayStateModifier.ON if not await self.device_connection.control_relays(states): return - self._is_on = True + self._attr_is_on = True self.async_write_ha_state() async def async_turn_off(self, **kwargs: Any) -> None: @@ -150,7 +140,7 @@ class LcnRelaySwitch(LcnEntity, SwitchEntity): states[self.output.value] = pypck.lcn_defs.RelayStateModifier.OFF if not await self.device_connection.control_relays(states): return - self._is_on = False + self._attr_is_on = False self.async_write_ha_state() def input_received(self, input_obj: InputType) -> None: @@ -158,5 +148,5 @@ class LcnRelaySwitch(LcnEntity, SwitchEntity): if not isinstance(input_obj, pypck.inputs.ModStatusRelays): return - self._is_on = input_obj.get_state(self.output.value) + self._attr_is_on = input_obj.get_state(self.output.value) self.async_write_ha_state()