Skip to content

Commit eef3472

Browse files
authored
KNX: Clean up internal setting of name, unique_id and entity_category for YAML entities (#160265)
1 parent f9bd9f4 commit eef3472

File tree

19 files changed

+240
-278
lines changed

19 files changed

+240
-278
lines changed

homeassistant/components/knx/binary_sensor.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -114,24 +114,26 @@ class KnxYamlBinarySensor(_KnxBinarySensor, KnxYamlEntity):
114114

115115
def __init__(self, knx_module: KNXModule, config: ConfigType) -> None:
116116
"""Initialize of KNX binary sensor."""
117+
self._device = XknxBinarySensor(
118+
xknx=knx_module.xknx,
119+
name=config[CONF_NAME],
120+
group_address_state=config[CONF_STATE_ADDRESS],
121+
invert=config[CONF_INVERT],
122+
sync_state=config[CONF_SYNC_STATE],
123+
ignore_internal_state=config[CONF_IGNORE_INTERNAL_STATE],
124+
context_timeout=config.get(CONF_CONTEXT_TIMEOUT),
125+
reset_after=config.get(CONF_RESET_AFTER),
126+
always_callback=True,
127+
)
117128
super().__init__(
118129
knx_module=knx_module,
119-
device=XknxBinarySensor(
120-
xknx=knx_module.xknx,
121-
name=config[CONF_NAME],
122-
group_address_state=config[CONF_STATE_ADDRESS],
123-
invert=config[CONF_INVERT],
124-
sync_state=config[CONF_SYNC_STATE],
125-
ignore_internal_state=config[CONF_IGNORE_INTERNAL_STATE],
126-
context_timeout=config.get(CONF_CONTEXT_TIMEOUT),
127-
reset_after=config.get(CONF_RESET_AFTER),
128-
always_callback=True,
129-
),
130+
unique_id=str(self._device.remote_value.group_address_state),
131+
name=config[CONF_NAME],
132+
entity_category=config.get(CONF_ENTITY_CATEGORY),
130133
)
131-
self._attr_entity_category = config.get(CONF_ENTITY_CATEGORY)
134+
132135
self._attr_device_class = config.get(CONF_DEVICE_CLASS)
133136
self._attr_force_update = self._device.ignore_internal_state
134-
self._attr_unique_id = str(self._device.remote_value.group_address_state)
135137

136138

137139
class KnxUiBinarySensor(_KnxBinarySensor, KnxUiEntity):

homeassistant/components/knx/button.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,18 @@ class KNXButton(KnxYamlEntity, ButtonEntity):
3535

3636
def __init__(self, knx_module: KNXModule, config: ConfigType) -> None:
3737
"""Initialize a KNX button."""
38-
super().__init__(
39-
knx_module=knx_module,
40-
device=XknxRawValue(
41-
xknx=knx_module.xknx,
42-
name=config[CONF_NAME],
43-
payload_length=config[CONF_PAYLOAD_LENGTH],
44-
group_address=config[KNX_ADDRESS],
45-
),
38+
self._device = XknxRawValue(
39+
xknx=knx_module.xknx,
40+
name=config[CONF_NAME],
41+
payload_length=config[CONF_PAYLOAD_LENGTH],
42+
group_address=config[KNX_ADDRESS],
4643
)
4744
self._payload = config[CONF_PAYLOAD]
48-
self._attr_entity_category = config.get(CONF_ENTITY_CATEGORY)
49-
self._attr_unique_id = (
50-
f"{self._device.remote_value.group_address}_{self._payload}"
45+
super().__init__(
46+
knx_module=knx_module,
47+
unique_id=f"{self._device.remote_value.group_address}_{self._payload}",
48+
name=config[CONF_NAME],
49+
entity_category=config.get(CONF_ENTITY_CATEGORY),
5150
)
5251

5352
async def async_press(self) -> None:

homeassistant/components/knx/climate.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ async def async_setup_entry(
119119
async_add_entities(entities)
120120

121121

122-
def _create_climate(xknx: XKNX, config: ConfigType) -> XknxClimate:
122+
def _create_climate_yaml(xknx: XKNX, config: ConfigType) -> XknxClimate:
123123
"""Return a KNX Climate device to be used within XKNX."""
124124
climate_mode = XknxClimateMode(
125125
xknx,
@@ -646,9 +646,17 @@ class KnxYamlClimate(_KnxClimate, KnxYamlEntity):
646646

647647
def __init__(self, knx_module: KNXModule, config: ConfigType) -> None:
648648
"""Initialize of a KNX climate device."""
649+
self._device = _create_climate_yaml(knx_module.xknx, config)
649650
super().__init__(
650651
knx_module=knx_module,
651-
device=_create_climate(knx_module.xknx, config),
652+
unique_id=(
653+
f"{self._device.temperature.group_address_state}_"
654+
f"{self._device.target_temperature.group_address_state}_"
655+
f"{self._device.target_temperature.group_address}_"
656+
f"{self._device._setpoint_shift.group_address}" # noqa: SLF001
657+
),
658+
name=config[CONF_NAME],
659+
entity_category=config.get(CONF_ENTITY_CATEGORY),
652660
)
653661
default_hvac_mode: HVACMode = config[ClimateConf.DEFAULT_CONTROLLER_MODE]
654662
fan_max_step = config[ClimateConf.FAN_MAX_STEP]
@@ -660,14 +668,6 @@ def __init__(self, knx_module: KNXModule, config: ConfigType) -> None:
660668
fan_zero_mode=fan_zero_mode,
661669
)
662670

663-
self._attr_entity_category = config.get(CONF_ENTITY_CATEGORY)
664-
self._attr_unique_id = (
665-
f"{self._device.temperature.group_address_state}_"
666-
f"{self._device.target_temperature.group_address_state}_"
667-
f"{self._device.target_temperature.group_address}_"
668-
f"{self._device._setpoint_shift.group_address}" # noqa: SLF001
669-
)
670-
671671

672672
class KnxUiClimate(_KnxClimate, KnxUiEntity):
673673
"""Representation of a KNX climate device configured from the UI."""

homeassistant/components/knx/cover.py

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -191,36 +191,34 @@ class KnxYamlCover(_KnxCover, KnxYamlEntity):
191191

192192
def __init__(self, knx_module: KNXModule, config: ConfigType) -> None:
193193
"""Initialize the cover."""
194+
self._device = XknxCover(
195+
xknx=knx_module.xknx,
196+
name=config[CONF_NAME],
197+
group_address_long=config.get(CoverSchema.CONF_MOVE_LONG_ADDRESS),
198+
group_address_short=config.get(CoverSchema.CONF_MOVE_SHORT_ADDRESS),
199+
group_address_stop=config.get(CoverSchema.CONF_STOP_ADDRESS),
200+
group_address_position_state=config.get(
201+
CoverSchema.CONF_POSITION_STATE_ADDRESS
202+
),
203+
group_address_angle=config.get(CoverSchema.CONF_ANGLE_ADDRESS),
204+
group_address_angle_state=config.get(CoverSchema.CONF_ANGLE_STATE_ADDRESS),
205+
group_address_position=config.get(CoverSchema.CONF_POSITION_ADDRESS),
206+
travel_time_down=config[CoverConf.TRAVELLING_TIME_DOWN],
207+
travel_time_up=config[CoverConf.TRAVELLING_TIME_UP],
208+
invert_updown=config[CoverConf.INVERT_UPDOWN],
209+
invert_position=config[CoverConf.INVERT_POSITION],
210+
invert_angle=config[CoverConf.INVERT_ANGLE],
211+
)
194212
super().__init__(
195213
knx_module=knx_module,
196-
device=XknxCover(
197-
xknx=knx_module.xknx,
198-
name=config[CONF_NAME],
199-
group_address_long=config.get(CoverSchema.CONF_MOVE_LONG_ADDRESS),
200-
group_address_short=config.get(CoverSchema.CONF_MOVE_SHORT_ADDRESS),
201-
group_address_stop=config.get(CoverSchema.CONF_STOP_ADDRESS),
202-
group_address_position_state=config.get(
203-
CoverSchema.CONF_POSITION_STATE_ADDRESS
204-
),
205-
group_address_angle=config.get(CoverSchema.CONF_ANGLE_ADDRESS),
206-
group_address_angle_state=config.get(
207-
CoverSchema.CONF_ANGLE_STATE_ADDRESS
208-
),
209-
group_address_position=config.get(CoverSchema.CONF_POSITION_ADDRESS),
210-
travel_time_down=config[CoverConf.TRAVELLING_TIME_DOWN],
211-
travel_time_up=config[CoverConf.TRAVELLING_TIME_UP],
212-
invert_updown=config[CoverConf.INVERT_UPDOWN],
213-
invert_position=config[CoverConf.INVERT_POSITION],
214-
invert_angle=config[CoverConf.INVERT_ANGLE],
214+
unique_id=(
215+
f"{self._device.updown.group_address}_"
216+
f"{self._device.position_target.group_address}"
215217
),
218+
name=config[CONF_NAME],
219+
entity_category=config.get(CONF_ENTITY_CATEGORY),
216220
)
217221
self.init_base()
218-
219-
self._attr_entity_category = config.get(CONF_ENTITY_CATEGORY)
220-
self._attr_unique_id = (
221-
f"{self._device.updown.group_address}_"
222-
f"{self._device.position_target.group_address}"
223-
)
224222
if custom_device_class := config.get(CONF_DEVICE_CLASS):
225223
self._attr_device_class = custom_device_class
226224

homeassistant/components/knx/date.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,20 +105,21 @@ class KnxYamlDate(_KNXDate, KnxYamlEntity):
105105

106106
def __init__(self, knx_module: KNXModule, config: ConfigType) -> None:
107107
"""Initialize a KNX date."""
108+
self._device = XknxDateDevice(
109+
knx_module.xknx,
110+
name=config[CONF_NAME],
111+
localtime=False,
112+
group_address=config[KNX_ADDRESS],
113+
group_address_state=config.get(CONF_STATE_ADDRESS),
114+
respond_to_read=config[CONF_RESPOND_TO_READ],
115+
sync_state=config[CONF_SYNC_STATE],
116+
)
108117
super().__init__(
109118
knx_module=knx_module,
110-
device=XknxDateDevice(
111-
knx_module.xknx,
112-
name=config[CONF_NAME],
113-
localtime=False,
114-
group_address=config[KNX_ADDRESS],
115-
group_address_state=config.get(CONF_STATE_ADDRESS),
116-
respond_to_read=config[CONF_RESPOND_TO_READ],
117-
sync_state=config[CONF_SYNC_STATE],
118-
),
119+
unique_id=str(self._device.remote_value.group_address),
120+
name=config[CONF_NAME],
121+
entity_category=config.get(CONF_ENTITY_CATEGORY),
119122
)
120-
self._attr_entity_category = config.get(CONF_ENTITY_CATEGORY)
121-
self._attr_unique_id = str(self._device.remote_value.group_address)
122123

123124

124125
class KnxUiDate(_KNXDate, KnxUiEntity):

homeassistant/components/knx/datetime.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,20 +110,21 @@ class KnxYamlDateTime(_KNXDateTime, KnxYamlEntity):
110110

111111
def __init__(self, knx_module: KNXModule, config: ConfigType) -> None:
112112
"""Initialize a KNX datetime."""
113+
self._device = XknxDateTimeDevice(
114+
knx_module.xknx,
115+
name=config[CONF_NAME],
116+
localtime=False,
117+
group_address=config[KNX_ADDRESS],
118+
group_address_state=config.get(CONF_STATE_ADDRESS),
119+
respond_to_read=config[CONF_RESPOND_TO_READ],
120+
sync_state=config[CONF_SYNC_STATE],
121+
)
113122
super().__init__(
114123
knx_module=knx_module,
115-
device=XknxDateTimeDevice(
116-
knx_module.xknx,
117-
name=config[CONF_NAME],
118-
localtime=False,
119-
group_address=config[KNX_ADDRESS],
120-
group_address_state=config.get(CONF_STATE_ADDRESS),
121-
respond_to_read=config[CONF_RESPOND_TO_READ],
122-
sync_state=config[CONF_SYNC_STATE],
123-
),
124+
unique_id=str(self._device.remote_value.group_address),
125+
name=config[CONF_NAME],
126+
entity_category=config.get(CONF_ENTITY_CATEGORY),
124127
)
125-
self._attr_entity_category = config.get(CONF_ENTITY_CATEGORY)
126-
self._attr_unique_id = str(self._device.remote_value.group_address)
127128

128129

129130
class KnxUiDateTime(_KNXDateTime, KnxUiEntity):

homeassistant/components/knx/entity.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from xknx.devices import Device as XknxDevice
88

9-
from homeassistant.const import CONF_ENTITY_CATEGORY, EntityCategory
9+
from homeassistant.const import CONF_ENTITY_CATEGORY, CONF_NAME, EntityCategory
1010
from homeassistant.helpers.device_registry import DeviceInfo
1111
from homeassistant.helpers.entity import Entity
1212
from homeassistant.helpers.entity_platform import EntityPlatform
@@ -52,14 +52,11 @@ class _KnxEntityBase(Entity):
5252
"""Representation of a KNX entity."""
5353

5454
_attr_should_poll = False
55+
56+
_attr_unique_id: str
5557
_knx_module: KNXModule
5658
_device: XknxDevice
5759

58-
@property
59-
def name(self) -> str:
60-
"""Return the name of the KNX device."""
61-
return self._device.name
62-
6360
@property
6461
def available(self) -> bool:
6562
"""Return True if entity is available."""
@@ -100,23 +97,32 @@ async def async_will_remove_from_hass(self) -> None:
10097
class KnxYamlEntity(_KnxEntityBase):
10198
"""Representation of a KNX entity configured from YAML."""
10299

103-
def __init__(self, knx_module: KNXModule, device: XknxDevice) -> None:
100+
def __init__(
101+
self,
102+
knx_module: KNXModule,
103+
unique_id: str,
104+
name: str,
105+
entity_category: EntityCategory | None,
106+
) -> None:
104107
"""Initialize the YAML entity."""
105108
self._knx_module = knx_module
106-
self._device = device
109+
self._attr_name = name or None
110+
self._attr_unique_id = unique_id
111+
self._attr_entity_category = entity_category
107112

108113

109114
class KnxUiEntity(_KnxEntityBase):
110115
"""Representation of a KNX UI entity."""
111116

112-
_attr_unique_id: str
113117
_attr_has_entity_name = True
114118

115119
def __init__(
116120
self, knx_module: KNXModule, unique_id: str, entity_config: dict[str, Any]
117121
) -> None:
118122
"""Initialize the UI entity."""
119123
self._knx_module = knx_module
124+
125+
self._attr_name = entity_config[CONF_NAME]
120126
self._attr_unique_id = unique_id
121127
if entity_category := entity_config.get(CONF_ENTITY_CATEGORY):
122128
self._attr_entity_category = EntityCategory(entity_category)

homeassistant/components/knx/fan.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -208,35 +208,32 @@ class KnxYamlFan(_KnxFan, KnxYamlEntity):
208208
def __init__(self, knx_module: KNXModule, config: ConfigType) -> None:
209209
"""Initialize of KNX fan."""
210210
max_step = config.get(FanConf.MAX_STEP)
211+
self._device = XknxFan(
212+
xknx=knx_module.xknx,
213+
name=config[CONF_NAME],
214+
group_address_speed=config.get(KNX_ADDRESS),
215+
group_address_speed_state=config.get(FanSchema.CONF_STATE_ADDRESS),
216+
group_address_oscillation=config.get(FanSchema.CONF_OSCILLATION_ADDRESS),
217+
group_address_oscillation_state=config.get(
218+
FanSchema.CONF_OSCILLATION_STATE_ADDRESS
219+
),
220+
group_address_switch=config.get(FanSchema.CONF_SWITCH_ADDRESS),
221+
group_address_switch_state=config.get(FanSchema.CONF_SWITCH_STATE_ADDRESS),
222+
max_step=max_step,
223+
sync_state=config.get(CONF_SYNC_STATE, True),
224+
)
211225
super().__init__(
212226
knx_module=knx_module,
213-
device=XknxFan(
214-
xknx=knx_module.xknx,
215-
name=config[CONF_NAME],
216-
group_address_speed=config.get(KNX_ADDRESS),
217-
group_address_speed_state=config.get(FanSchema.CONF_STATE_ADDRESS),
218-
group_address_oscillation=config.get(
219-
FanSchema.CONF_OSCILLATION_ADDRESS
220-
),
221-
group_address_oscillation_state=config.get(
222-
FanSchema.CONF_OSCILLATION_STATE_ADDRESS
223-
),
224-
group_address_switch=config.get(FanSchema.CONF_SWITCH_ADDRESS),
225-
group_address_switch_state=config.get(
226-
FanSchema.CONF_SWITCH_STATE_ADDRESS
227-
),
228-
max_step=max_step,
229-
sync_state=config.get(CONF_SYNC_STATE, True),
227+
unique_id=(
228+
str(self._device.speed.group_address)
229+
if self._device.speed.group_address
230+
else str(self._device.switch.group_address)
230231
),
232+
name=config[CONF_NAME],
233+
entity_category=config.get(CONF_ENTITY_CATEGORY),
231234
)
232235
# FanSpeedMode.STEP if max_step is set
233236
self._step_range: tuple[int, int] | None = (1, max_step) if max_step else None
234-
self._attr_entity_category = config.get(CONF_ENTITY_CATEGORY)
235-
236-
if self._device.speed.group_address:
237-
self._attr_unique_id = str(self._device.speed.group_address)
238-
else:
239-
self._attr_unique_id = str(self._device.switch.group_address)
240237

241238

242239
class KnxUiFan(_KnxFan, KnxUiEntity):

homeassistant/components/knx/light.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -558,15 +558,16 @@ class KnxYamlLight(_KnxLight, KnxYamlEntity):
558558

559559
def __init__(self, knx_module: KNXModule, config: ConfigType) -> None:
560560
"""Initialize of KNX light."""
561+
self._device = _create_yaml_light(knx_module.xknx, config)
561562
super().__init__(
562563
knx_module=knx_module,
563-
device=_create_yaml_light(knx_module.xknx, config),
564+
unique_id=self._device_unique_id(),
565+
name=config[CONF_NAME],
566+
entity_category=config.get(CONF_ENTITY_CATEGORY),
564567
)
565568
self._attr_color_mode = next(iter(self.supported_color_modes))
566569
self._attr_max_color_temp_kelvin: int = config[LightSchema.CONF_MAX_KELVIN]
567570
self._attr_min_color_temp_kelvin: int = config[LightSchema.CONF_MIN_KELVIN]
568-
self._attr_entity_category = config.get(CONF_ENTITY_CATEGORY)
569-
self._attr_unique_id = self._device_unique_id()
570571

571572
def _device_unique_id(self) -> str:
572573
"""Return unique id for this device."""

homeassistant/components/knx/notify.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,13 @@ class KNXNotify(KnxYamlEntity, NotifyEntity):
4646

4747
def __init__(self, knx_module: KNXModule, config: ConfigType) -> None:
4848
"""Initialize a KNX notification."""
49+
self._device = _create_notification_instance(knx_module.xknx, config)
4950
super().__init__(
5051
knx_module=knx_module,
51-
device=_create_notification_instance(knx_module.xknx, config),
52+
unique_id=str(self._device.remote_value.group_address),
53+
name=config[CONF_NAME],
54+
entity_category=config.get(CONF_ENTITY_CATEGORY),
5255
)
53-
self._attr_entity_category = config.get(CONF_ENTITY_CATEGORY)
54-
self._attr_unique_id = str(self._device.remote_value.group_address)
5556

5657
async def async_send_message(self, message: str, title: str | None = None) -> None:
5758
"""Send a notification to knx bus."""

0 commit comments

Comments
 (0)