diff --git a/custom_components/ac_infinity/const.py b/custom_components/ac_infinity/const.py index f04253c..36194fa 100644 --- a/custom_components/ac_infinity/const.py +++ b/custom_components/ac_infinity/const.py @@ -1,5 +1,4 @@ """Constants for the AC Infinity integration.""" - from homeassistant.const import Platform MANUFACTURER = "AC Infinity" @@ -16,6 +15,7 @@ CONF_POLLING_INTERVAL = "polling_interval" CONF_UPDATE_PASSWORD = "update_password" DEFAULT_POLLING_INTERVAL = 10 +ISSUE_URL = "https://github.com/dalinicus/homeassistant-acinfinity/issues/new?template=Blank+issue" class CustomPortPropertyKey: @@ -58,16 +58,29 @@ class SensorPropertyKey: class SensorType: - PROBE_TEMPERATURE = 0 + PROBE_TEMPERATURE_F = 0 + PROBE_TEMPERATURE_C = 1 PROBE_HUMIDITY = 2 PROBE_VPD = 3 - EXTERNAL_TEMPERATURE = 4 + EXTERNAL_TEMPERATURE_F = 4 + EXTERNAL_TEMPERATURE_C = 4 EXTERNAL_HUMIDITY = 6 EXTERNAL_VPD = 7 CO2 = 11 LIGHT = 12 +class SensorKeys: + PROBE_TEMPERATURE = "probeTemperature" + PROBE_HUMIDITY = "probeHumidity" + PROBE_VPD = "probeVaporPressureDeficit" + EXTERNAL_TEMPERATURE = "externalTemperature" + EXTERNAL_HUMIDITY = "externalHumidity" + EXTERNAL_VPD = "externalVaporPressureDeficit" + CO2_SENSOR = "co2Sensor" + LIGHT_SENSOR = "lightSensor" + + # noinspection SpellCheckingInspection class PortPropertyKey: # /api/dev/devInfoListAll via "ports" property diff --git a/custom_components/ac_infinity/core.py b/custom_components/ac_infinity/core.py index 7660ba9..1825de4 100644 --- a/custom_components/ac_infinity/core.py +++ b/custom_components/ac_infinity/core.py @@ -161,7 +161,7 @@ def __get_device_info( controller: ACInfinityController, sensor_port: int, sensor_type: int ): match sensor_type: - case SensorType.PROBE_TEMPERATURE | SensorType.PROBE_HUMIDITY | SensorType.PROBE_VPD: + case SensorType.PROBE_TEMPERATURE_F | SensorType.PROBE_TEMPERATURE_C | SensorType.PROBE_HUMIDITY | SensorType.PROBE_VPD: return DeviceInfo( identifiers={ (DOMAIN, f"{controller.device_id}_{sensor_port}_spc24") @@ -181,7 +181,7 @@ def __get_device_info( via_device=controller.identifier, model="UIS CO2 + Light Sensor (AC-COS3)", ) - case SensorType.EXTERNAL_TEMPERATURE | SensorType.EXTERNAL_HUMIDITY | SensorType.EXTERNAL_VPD: + case SensorType.EXTERNAL_TEMPERATURE_F | SensorType.EXTERNAL_HUMIDITY | SensorType.EXTERNAL_VPD: return controller.device_info case _: return DeviceInfo( @@ -912,7 +912,7 @@ def __init__( @property def unique_id(self) -> str: """Return the unique ID for this entity.""" - return f"{DOMAIN}_{self._sensor.controller.mac_addr}_port_{self._sensor.sensor_port}_{self._sensor.sensor_type}" + return f"{DOMAIN}_{self._sensor.controller.mac_addr}_sensor_{self._sensor.sensor_port}_{self.entity_description.key}" @property def device_info(self) -> DeviceInfo: diff --git a/custom_components/ac_infinity/sensor.py b/custom_components/ac_infinity/sensor.py index ab2640e..367e459 100644 --- a/custom_components/ac_infinity/sensor.py +++ b/custom_components/ac_infinity/sensor.py @@ -40,9 +40,11 @@ from .const import ( DOMAIN, + ISSUE_URL, ControllerPropertyKey, CustomPortPropertyKey, PortPropertyKey, + SensorKeys, SensorPropertyKey, SensorType, ) @@ -265,9 +267,19 @@ def __get_next_mode_change_timestamp( ] SENSOR_DESCRIPTIONS: dict[int, ACInfinitySensorSensorEntityDescription] = { - SensorType.PROBE_TEMPERATURE: ACInfinitySensorSensorEntityDescription( - # key is not used for sensors. - key="probeTemperature", + SensorType.PROBE_TEMPERATURE_F: ACInfinitySensorSensorEntityDescription( + key=SensorKeys.PROBE_TEMPERATURE, + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + icon=None, # default + translation_key="probe_temperature", + suggested_unit_of_measurement=None, + suitable_fn=__suitable_fn_sensor_temperature, + get_value_fn=__get_value_fn_sensor_value_temperature, + ), + SensorType.PROBE_TEMPERATURE_C: ACInfinitySensorSensorEntityDescription( + key=SensorKeys.PROBE_TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE, state_class=SensorStateClass.MEASUREMENT, native_unit_of_measurement=UnitOfTemperature.CELSIUS, @@ -278,7 +290,7 @@ def __get_next_mode_change_timestamp( get_value_fn=__get_value_fn_sensor_value_temperature, ), SensorType.PROBE_HUMIDITY: ACInfinitySensorSensorEntityDescription( - key="probeHumidity", + key=SensorKeys.PROBE_HUMIDITY, device_class=SensorDeviceClass.HUMIDITY, state_class=SensorStateClass.MEASUREMENT, native_unit_of_measurement=PERCENTAGE, @@ -289,7 +301,7 @@ def __get_next_mode_change_timestamp( get_value_fn=__get_value_fn_sensor_value_default, ), SensorType.PROBE_VPD: ACInfinitySensorSensorEntityDescription( - key="probeVaporPressureDeficit", + key=SensorKeys.PROBE_VPD, device_class=SensorDeviceClass.PRESSURE, state_class=SensorStateClass.MEASUREMENT, suggested_unit_of_measurement=UnitOfPressure.KPA, @@ -299,8 +311,19 @@ def __get_next_mode_change_timestamp( suitable_fn=__suitable_fn_sensor_default, get_value_fn=__get_value_fn_sensor_value_default, ), - SensorType.EXTERNAL_TEMPERATURE: ACInfinitySensorSensorEntityDescription( - key="externalTemperature", + SensorType.EXTERNAL_TEMPERATURE_F: ACInfinitySensorSensorEntityDescription( + key=SensorKeys.EXTERNAL_TEMPERATURE, + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + icon=None, # default + translation_key="external_temperature", + suggested_unit_of_measurement=None, + suitable_fn=__suitable_fn_sensor_temperature, + get_value_fn=__get_value_fn_sensor_value_temperature, + ), + SensorType.EXTERNAL_TEMPERATURE_C: ACInfinitySensorSensorEntityDescription( + key=SensorKeys.EXTERNAL_TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE, state_class=SensorStateClass.MEASUREMENT, native_unit_of_measurement=UnitOfTemperature.CELSIUS, @@ -311,7 +334,7 @@ def __get_next_mode_change_timestamp( get_value_fn=__get_value_fn_sensor_value_temperature, ), SensorType.EXTERNAL_HUMIDITY: ACInfinitySensorSensorEntityDescription( - key="externalHumidity", + key=SensorKeys.EXTERNAL_HUMIDITY, device_class=SensorDeviceClass.HUMIDITY, state_class=SensorStateClass.MEASUREMENT, native_unit_of_measurement=PERCENTAGE, @@ -322,7 +345,7 @@ def __get_next_mode_change_timestamp( get_value_fn=__get_value_fn_sensor_value_default, ), SensorType.EXTERNAL_VPD: ACInfinitySensorSensorEntityDescription( - key="externalVaporPressureDeficit", + key=SensorKeys.EXTERNAL_VPD, device_class=SensorDeviceClass.PRESSURE, state_class=SensorStateClass.MEASUREMENT, suggested_unit_of_measurement=UnitOfPressure.KPA, @@ -333,7 +356,7 @@ def __get_next_mode_change_timestamp( get_value_fn=__get_value_fn_sensor_value_default, ), SensorType.CO2: ACInfinitySensorSensorEntityDescription( - key="co2Sensor", + key=SensorKeys.CO2_SENSOR, device_class=SensorDeviceClass.CO2, state_class=SensorStateClass.MEASUREMENT, native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION, @@ -344,7 +367,7 @@ def __get_next_mode_change_timestamp( get_value_fn=__get_value_fn_sensor_value_default, ), SensorType.LIGHT: ACInfinitySensorSensorEntityDescription( - key="lightSensor", + key=SensorKeys.LIGHT_SENSOR, device_class=SensorDeviceClass.ILLUMINANCE, state_class=SensorStateClass.MEASUREMENT, native_unit_of_measurement=LIGHT_LUX, @@ -470,9 +493,16 @@ async def async_setup_entry( entities.append_if_suitable(entity) for sensor in controller.sensors: - description = SENSOR_DESCRIPTIONS[sensor.sensor_type] - entity = ACInfinitySensorSensorEntity(coordinator, description, sensor) - entities.append_if_suitable(entity) + if sensor.sensor_type in SENSOR_DESCRIPTIONS: + description = SENSOR_DESCRIPTIONS[sensor.sensor_type] + entity = ACInfinitySensorSensorEntity(coordinator, description, sensor) + entities.append_if_suitable(entity) + else: + logging.warning( + 'Unknown sensor type "%s". Please fill out an issue at %s with this error message.', + sensor.sensor_type, + ISSUE_URL, + ) for port in controller.ports: for description in PORT_DESCRIPTIONS: