Skip to content

Commit

Permalink
Add ACTIVE_PROFILE to IPThermostat (#86)
Browse files Browse the repository at this point in the history
* Add ACTIVE_PROFILE to IPThermostat

* Cleanup API, device/entity

* Update entity.py
  • Loading branch information
SukramJ authored Dec 23, 2021
1 parent bf8acb8 commit 89fac98
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 69 deletions.
4 changes: 4 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Version 0.3.0 (2021-12-23)
- Cleanup API, device/entity
- Add ACTIVE_PROFILE to IPThermostat

Version 0.2.0 (2021-12-22)
- Cleanup API, reduce visibility
- Add setValue to client
Expand Down
2 changes: 1 addition & 1 deletion hahomematic/central_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ async def set_value(
value: Any,
rx_mode: str | None = None,
) -> None:
""""Set single value on paramset VALUES."""
"""Set single value on paramset VALUES."""

if client := self.get_primary_client(interface_id):
await client.set_value(
Expand Down
5 changes: 3 additions & 2 deletions hahomematic/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,9 @@ async def get_install_mode(self) -> Any:
# except ProxyException:
# _LOGGER.exception("list_bidcos_interfaces: ProxyException")

async def set_value(self, address: str, value_key: str, value: Any, rx_mode: str | None = None) -> None:
async def set_value(
self, address: str, value_key: str, value: Any, rx_mode: str | None = None
) -> None:
"""Set single value on paramset VALUES."""
try:
if rx_mode:
Expand All @@ -263,7 +265,6 @@ async def set_value(self, address: str, value_key: str, value: Any, rx_mode: str
except ProxyException:
_LOGGER.exception("set_value: ProxyException")


async def put_paramset(
self, address: str, paramset: str, value: Any, rx_mode: str | None = None
) -> None:
Expand Down
108 changes: 63 additions & 45 deletions hahomematic/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,17 @@ def __init__(
"""
Initialize the device object.
"""
self.central = central
self.interface_id = interface_id
self.client = self.central.clients[self.interface_id]
self.address = address
self.channels = self.central.raw_devices.get_channels(
self.interface_id, self.address
self._central = central
self._interface_id = interface_id
self._client = self._central.clients[self._interface_id]
self._address = address
self._channels = self._central.raw_devices.get_channels(
self._interface_id, self._address
)
_LOGGER.debug(
"Device.__init__: Initializing device: %s, %s",
self.interface_id,
self.address,
self._interface_id,
self._address,
)

self.entities: dict[tuple[str, str], GenericEntity] = {}
Expand All @@ -97,43 +97,58 @@ def __init__(
self._update_callbacks: list[Callable] = []
self._remove_callbacks: list[Callable] = []
self.device_type: str = str(
self.central.raw_devices.get_device_parameter(
self.interface_id, self.address, ATTR_HM_TYPE
self._central.raw_devices.get_device_parameter(
self._interface_id, self._address, ATTR_HM_TYPE
)
)
self.sub_type: str = str(
self.central.raw_devices.get_device_parameter(
self.interface_id, self.address, ATTR_HM_SUBTYPE
self._central.raw_devices.get_device_parameter(
self._interface_id, self._address, ATTR_HM_SUBTYPE
)
)
# marker if device will be created as custom entity
self.is_custom_entity: bool = entity_definition_exists(
self.device_type, self.sub_type
)
self.firmware: str = str(
self.central.raw_devices.get_device_parameter(
self.interface_id, self.address, ATTR_HM_FIRMWARE
self._central.raw_devices.get_device_parameter(
self._interface_id, self._address, ATTR_HM_FIRMWARE
)
)

if name := self.central.names.get_name(self.address):
if name := self._central.names.get_name(self._address):
self.name = name
else:
_LOGGER.info(
"Device.__init__: Using auto-generated name for %s %s",
self.device_type,
self.address,
self._address,
)
self.name = f"{self.device_type}_{self.address}"
self.name = f"{self.device_type}_{self._address}"

_LOGGER.debug(
"Device.__init__: Initialized device: %s, %s, %s, %s",
self.interface_id,
self.address,
self._interface_id,
self._address,
self.device_type,
self.name,
)

@property
def central(self) -> hm_central.CentralUnit:
"""Return the central unit."""
return self._central

@property
def interface_id(self) -> str:
"""Return the interface_id."""
return self._interface_id

@property
def address(self) -> str:
"""Return the address."""
return self._address

def add_hm_entity(self, hm_entity: BaseEntity) -> None:
"""Add a hm entity to a device."""
if isinstance(hm_entity, GenericEntity):
Expand Down Expand Up @@ -211,28 +226,31 @@ def __str__(self) -> str:
"""
Provide some useful information.
"""
return f"address: {self.address}, type: {self.device_type}, name: {self.name}, entities: {self.entities}"
return f"address: {self._address}, type: {self.device_type}, name: {self.name}, entities: {self.entities}"

@property
def device_info(self) -> dict[str, Any]:
"""Return device specific attributes."""
return {
"identifiers": {
(HA_DOMAIN, f"{self.address}{IDENTIFIERS_SEPARATOR}{self.interface_id}")
(
HA_DOMAIN,
f"{self._address}{IDENTIFIERS_SEPARATOR}{self._interface_id}",
)
},
"name": self.name,
"manufacturer": MANUFACTURER,
"model": self.device_type,
"sw_version": self.firmware,
"via_device": (HA_DOMAIN, self.central.instance_name),
"via_device": (HA_DOMAIN, self._central.instance_name),
}

@property
def available(self) -> bool:
"""Return the availability of the device."""
if self._available is False:
return False
un_reach = self.action_events.get((f"{self.address}:0", PARAM_UN_REACH))
un_reach = self.action_events.get((f"{self._address}:0", PARAM_UN_REACH))
if un_reach and un_reach.last_update:
return not un_reach.value
return True
Expand All @@ -248,7 +266,7 @@ async def reload_paramsets(self) -> None:
"""Reload paramset for device."""
for entity in self.entities.values():
for paramset in RELEVANT_PARAMSETS:
await self.client.fetch_paramset(entity.address, paramset)
await self._client.fetch_paramset(entity.address, paramset)
entity.update_parameter_data()
self.update_device()

Expand All @@ -258,25 +276,25 @@ def create_entities(self) -> set[BaseEntity]:
Create the entities associated to this device.
"""
new_entities: list[BaseEntity] = []
for channel in self.channels:
if not self.central.paramsets.get_by_interface_address(
self.interface_id, channel
for channel in self._channels:
if not self._central.paramsets.get_by_interface_address(
self._interface_id, channel
):
_LOGGER.debug(
"Device.create_entities: Skipping channel %s, missing paramsets.",
channel,
)
continue
for paramset in self.central.paramsets.get_by_interface_address(
self.interface_id, channel
for paramset in self._central.paramsets.get_by_interface_address(
self._interface_id, channel
):
if paramset != PARAMSET_VALUES:
continue
for (
parameter,
parameter_data,
) in self.central.paramsets.get_by_interface_address_paramset(
self.interface_id, channel, paramset
) in self._central.paramsets.get_by_interface_address_paramset(
self._interface_id, channel, paramset
).items():
entity: GenericEntity | None
if (
Expand All @@ -298,7 +316,7 @@ def create_entities(self) -> set[BaseEntity]:
parameter=parameter,
parameter_data=parameter_data,
)
if self.address.startswith(tuple(HM_VIRTUAL_REMOTES)):
if self._address.startswith(tuple(HM_VIRTUAL_REMOTES)):
entity = self.create_button(
address=channel,
parameter=parameter,
Expand All @@ -318,8 +336,8 @@ def create_entities(self) -> set[BaseEntity]:
if self.is_custom_entity:
_LOGGER.debug(
"Device.create_entities: Handling custom entity integration: %s, %s, %s",
self.interface_id,
self.address,
self._interface_id,
self._address,
self.device_type,
)
# Call the custom creation function.
Expand All @@ -328,7 +346,7 @@ def create_entities(self) -> set[BaseEntity]:
self.device_type, self.sub_type
):
custom_entities: list[CustomEntity] = device_func(
self, self.address, group_base_channels
self, self._address, group_base_channels
)
new_entities.extend(custom_entities)
return set(new_entities)
Expand All @@ -338,13 +356,13 @@ def create_button(
) -> HmButton | None:
"""Create the buttons associated to this device"""
unique_id = generate_unique_id(
address, parameter, f"button_{self.central.instance_name}"
address, parameter, f"button_{self._central.instance_name}"
)
_LOGGER.debug(
"create_event: Creating button for %s, %s, %s",
address,
parameter,
self.interface_id,
self._interface_id,
)

if button := HmButton(
Expand All @@ -362,18 +380,18 @@ def create_event(
self, address: str, parameter: str, parameter_data: dict[str, Any]
) -> BaseEvent | None:
"""Create action event entity."""
if (address, parameter) not in self.central.entity_event_subscriptions:
self.central.entity_event_subscriptions[(address, parameter)] = []
if (address, parameter) not in self._central.entity_event_subscriptions:
self._central.entity_event_subscriptions[(address, parameter)] = []

unique_id = generate_unique_id(
address, parameter, f"event_{self.central.instance_name}"
address, parameter, f"event_{self._central.instance_name}"
)

_LOGGER.debug(
"create_event: Creating event for %s, %s, %s",
address,
parameter,
self.interface_id,
self._interface_id,
)
action_event: BaseEvent | None = None
if parameter_data[ATTR_HM_OPERATIONS] & OPERATION_EVENT:
Expand Down Expand Up @@ -421,18 +439,18 @@ def create_entity(
"create_entity: Ignoring parameter: %s (%s)", parameter, address
)
return None
if (address, parameter) not in self.central.entity_event_subscriptions:
self.central.entity_event_subscriptions[(address, parameter)] = []
if (address, parameter) not in self._central.entity_event_subscriptions:
self._central.entity_event_subscriptions[(address, parameter)] = []

unique_id = generate_unique_id(address, parameter)
if unique_id in self.central.hm_entities:
if unique_id in self._central.hm_entities:
_LOGGER.debug("create_entity: Skipping %s (already exists)", unique_id)
return None
_LOGGER.debug(
"create_entity: Creating entity for %s, %s, %s",
address,
parameter,
self.interface_id,
self._interface_id,
)
entity: GenericEntity | None = None
if parameter_data[ATTR_HM_OPERATIONS] & OPERATION_WRITE:
Expand Down
2 changes: 2 additions & 0 deletions hahomematic/devices/entity_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
ED_VIRT_CHANNEL = "virt_channel"
DEFAULT_INCLUDE_DEFAULT_ENTITIES = True

FIELD_ACTIVE_PROFILE = "active_profile"
FIELD_AUTO_MODE = "auto_mode"
FIELD_BOOST_MODE = "boost_mode"
FIELD_CHANNEL_COLOR = "channel_color"
Expand Down Expand Up @@ -252,6 +253,7 @@ def __str__(self) -> str:
},
ED_ADDITIONAL_ENTITIES: {
1: {
FIELD_ACTIVE_PROFILE: "ACTIVE_PROFILE",
FIELD_HUMIDITY: "HUMIDITY",
FIELD_LEVEL: "LEVEL",
},
Expand Down
10 changes: 5 additions & 5 deletions hahomematic/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ def __init__(
self.device_type: str = self._device.device_type
self.sub_type: str = self._device.sub_type
self.create_in_ha: bool = not self._device.is_custom_entity
self.client: hm_client.Client = self._central.clients[self._interface_id]
self.proxy: hm_proxy.XmlRpcProxy = self.client.proxy
self._client: hm_client.Client = self._central.clients[self._interface_id]
self._proxy: hm_proxy.XmlRpcProxy = self._client.proxy
self.name: str = self._central.names.get_name(self.address) or self.unique_id

@property
Expand Down Expand Up @@ -258,7 +258,7 @@ def hmtype(self) -> str | None:
async def send_value(self, value: Any) -> None:
"""send value to ccu."""
try:
await self.proxy.setValue(self.address, self.parameter, value)
await self._proxy.setValue(self.address, self.parameter, value)
except Exception:
_LOGGER.exception(
"generic_entity: Failed to set state for: %s, %s, %s, %s",
Expand Down Expand Up @@ -365,7 +365,7 @@ async def load_data(self) -> int:
return DATA_NO_LOAD
try:
if self._operations & OPERATION_READ:
self._state = await self.proxy.getValue(self.address, self.parameter)
self._state = await self._proxy.getValue(self.address, self.parameter)
self.update_entity()

self.update_entity(self.unique_id)
Expand Down Expand Up @@ -599,7 +599,7 @@ def value(self) -> Any:
async def send_value(self, value: Any) -> None:
"""Send value to ccu."""
try:
await self.proxy.setValue(self.address, self.parameter, value)
await self._proxy.setValue(self.address, self.parameter, value)
except Exception:
_LOGGER.exception(
"action_event: Failed to set state for: %s, %s, %s",
Expand Down
Loading

0 comments on commit 89fac98

Please sign in to comment.