Skip to content

Commit

Permalink
Provide defaults for all get lambda calls to prevent None errors. (#83)
Browse files Browse the repository at this point in the history
* write tests and fix functionality.

* bump

* precheck issues
  • Loading branch information
dalinicus authored Nov 4, 2024
1 parent a411801 commit ea3ef0b
Show file tree
Hide file tree
Showing 12 changed files with 527 additions and 410 deletions.
47 changes: 37 additions & 10 deletions custom_components/ac_infinity/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@
ACInfinityControllerReadOnlyMixin,
ACInfinityDataUpdateCoordinator,
ACInfinityEntities,
ACInfinityEntity,
ACInfinityPort,
ACInfinityPortEntity,
ACInfinityPortReadOnlyMixin,
get_value_fn_controller_property_default,
get_value_fn_port_property_default,
suitable_fn_controller_property_default,
suitable_fn_port_property_default,
)

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -58,14 +55,44 @@ class ACInfinityPortBinarySensorEntityDescription(
"""Describes ACInfinity Binary Sensor Port Entities."""


def __suitable_fn_controller_property_default(
entity: ACInfinityEntity, controller: ACInfinityController
):
return entity.ac_infinity.get_controller_property_exists(
controller.device_id, entity.entity_description.key
)


def __suitable_fn_port_property_default(entity: ACInfinityEntity, port: ACInfinityPort):
return entity.ac_infinity.get_port_property_exists(
port.controller.device_id, port.port_index, entity.entity_description.key
)


def __get_value_fn_controller_property_default(
entity: ACInfinityEntity, controller: ACInfinityController
):
return entity.ac_infinity.get_controller_property(
controller.device_id, entity.entity_description.key, False
)


def __get_value_fn_port_property_default(
entity: ACInfinityEntity, port: ACInfinityPort
):
return entity.ac_infinity.get_port_property(
port.controller.device_id, port.port_index, entity.entity_description.key, False
)


CONTROLLER_DESCRIPTIONS: list[ACInfinityControllerBinarySensorEntityDescription] = [
ACInfinityControllerBinarySensorEntityDescription(
key=ControllerPropertyKey.ONLINE,
device_class=BinarySensorDeviceClass.CONNECTIVITY,
icon="mdi:power-plug",
translation_key="controller_online",
suitable_fn=suitable_fn_controller_property_default,
get_value_fn=get_value_fn_controller_property_default,
suitable_fn=__suitable_fn_controller_property_default,
get_value_fn=__get_value_fn_controller_property_default,
)
]

Expand All @@ -75,16 +102,16 @@ class ACInfinityPortBinarySensorEntityDescription(
device_class=BinarySensorDeviceClass.CONNECTIVITY,
icon="mdi:power-plug",
translation_key="port_online",
suitable_fn=suitable_fn_port_property_default,
get_value_fn=get_value_fn_port_property_default,
suitable_fn=__suitable_fn_port_property_default,
get_value_fn=__get_value_fn_port_property_default,
),
ACInfinityPortBinarySensorEntityDescription(
key=PortPropertyKey.STATE,
device_class=BinarySensorDeviceClass.POWER,
icon="mdi:power",
translation_key="port_state",
suitable_fn=suitable_fn_port_property_default,
get_value_fn=get_value_fn_port_property_default,
suitable_fn=__suitable_fn_port_property_default,
get_value_fn=__get_value_fn_port_property_default,
),
]

Expand Down
92 changes: 0 additions & 92 deletions custom_components/ac_infinity/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,98 +784,6 @@ class ACInfinityPortReadWriteMixin(ACInfinityPortReadOnlyMixin):
"""Input data object, device id, port number, and desired value."""


def suitable_fn_controller_property_default(
entity: ACInfinityEntity, controller: ACInfinityController
):
return entity.ac_infinity.get_controller_property_exists(
controller.device_id, entity.entity_description.key
)


def get_value_fn_controller_property_default(
entity: ACInfinityEntity, controller: ACInfinityController
):
return entity.ac_infinity.get_controller_property(
controller.device_id, entity.entity_description.key
)


def suitable_fn_port_property_default(entity: ACInfinityEntity, port: ACInfinityPort):
return entity.ac_infinity.get_port_property_exists(
port.controller.device_id, port.port_index, entity.entity_description.key
)


def get_value_fn_port_property_default(entity: ACInfinityEntity, port: ACInfinityPort):
return entity.ac_infinity.get_port_property(
port.controller.device_id, port.port_index, entity.entity_description.key
)


def suitable_fn_controller_setting_default(
entity: ACInfinityEntity, controller: ACInfinityController
):
return entity.ac_infinity.get_controller_setting_exists(
controller.device_id, entity.entity_description.key
)


def get_value_fn_controller_setting_default(
entity: ACInfinityEntity, controller: ACInfinityController
):
return entity.ac_infinity.get_controller_setting(
controller.device_id, entity.entity_description.key
)


def set_value_fn_controller_setting_default(
entity: ACInfinityEntity, controller: ACInfinityController, value: int
):
return entity.ac_infinity.update_controller_setting(
controller.device_id, entity.entity_description.key, value
)


def suitable_fn_port_control_default(entity: ACInfinityEntity, port: ACInfinityPort):
return entity.ac_infinity.get_port_control_exists(
port.controller.device_id, port.port_index, entity.entity_description.key
)


def get_value_fn_port_control_default(entity: ACInfinityEntity, port: ACInfinityPort):
return entity.ac_infinity.get_port_control(
port.controller.device_id, port.port_index, entity.entity_description.key
)


def set_value_fn_port_control_default(
entity: ACInfinityEntity, port: ACInfinityPort, value: int
):
return entity.ac_infinity.update_port_control(
port.controller.device_id, port.port_index, entity.entity_description.key, value
)


def suitable_fn_port_setting_default(entity: ACInfinityEntity, port: ACInfinityPort):
return entity.ac_infinity.get_port_setting_exists(
port.controller.device_id, port.port_index, entity.entity_description.key
)


def get_value_fn_port_setting_default(entity: ACInfinityEntity, port: ACInfinityPort):
return entity.ac_infinity.get_port_setting(
port.controller.device_id, port.port_index, entity.entity_description.key
)


def set_value_fn_port_setting_default(
entity: ACInfinityEntity, port: ACInfinityPort, value: int
):
return entity.ac_infinity.update_port_setting(
port.controller.device_id, port.port_index, entity.entity_description.key, value
)


class ACInfinityEntities(list[ACInfinityEntity]):
def append_if_suitable(self, entity: ACInfinityEntity):
if entity.is_suitable:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/ac_infinity/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/dalinicus/homeassistant-acinfinity",
"requirements": [],
"version": "1.8.0"
"version": "1.8.1"
}
Loading

0 comments on commit ea3ef0b

Please sign in to comment.