Skip to content

Commit

Permalink
Merge pull request #3 from niels-ole-gram/main
Browse files Browse the repository at this point in the history
Added support for Comfort 600
  • Loading branch information
frodef authored Sep 11, 2023
2 parents 9e0c68a + 8d1b001 commit 89654e3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 51 deletions.
7 changes: 4 additions & 3 deletions custom_components/nilan_cts600/nilan_cts600.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,10 +453,10 @@ def scanSequence (menu_spec_sequence):
display = run_action(e['display']) if 'display' in e else self.display()
# print (f"scan: {e}, disp {display}")
if 'regexp' in e:
if not (match := re.match (e['regexp'], display)):
if not (match := re.match (e['regexp'], display)) and ('default' not in e):
break # Stop sequence at first mismatch
else:
record_matching_entry (match.groupdict(), e)
record_matching_entry (match.groupdict() if match else { 'value': e['default'] }, e)
if 'gonext' in e:
run_action(e['gonext'])
else:
Expand Down Expand Up @@ -502,8 +502,9 @@ def scanData (self, updateShowData=True, updateAllData=False):
scan_menu = [
_scanner_reset_menu(),
f (regexp="(?P<value>.*)", var='display', parse=lambda d: d.replace ('/', '\n')),
f (regexp=".* (?P<value>\d+)°C", var='thermostat', parse=int),
f (regexp=".* (?P<value>\d+)°C", var='thermostat', parse=int, kind='temperature'),
f (regexp="^(?P<value>\w+)", var='mode'),
f (regexp="^\w+\s+(?P<value>\w)", var='program', default=None),
f (regexp=".*>(?P<value>\d+)<", var='flow', parse=int)
]
if updateShowData:
Expand Down
77 changes: 29 additions & 48 deletions custom_components/nilan_cts600/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,47 +20,28 @@

_LOGGER = logging.getLogger(__name__)

_ENTITIES = (
SensorEntityDescription(
key="T1",
name="T1",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
SensorEntityDescription(
key="T2",
name="T2",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
SensorEntityDescription(
key="T5",
name="T5",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
SensorEntityDescription(
key="T6",
name="T6",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
SensorEntityDescription(
key="T15",
name="T5",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
SensorEntityDescription(
key="display",
name="display"
)
)
def discover_sensors(cts600):
data = cts600.data
metaData = cts600.metaData
sensors = []

for e in data:
sed = SensorEntityDescription(key = e, name = e.replace('_', ' ').lower())
description = metaData[e]['description'] if e in metaData and 'description' in metaData[e] else None
kind = metaData[e]['kind'] if e in metaData and 'kind' in metaData[e] else None

if description:
sed.name = sed.name + " (" + description.replace('_', ' ').capitalize() + ")"

if kind == 'temperature':
sed.name = sed.name[0].upper() + sed.name[1:]
sed.state_class = SensorStateClass.MEASUREMENT
sed.device_class = SensorDeviceClass.TEMPERATURE
sed.native_unit_of_measurement = UnitOfTemperature.CELSIUS

sensors.append(sed)

return sensors

async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback) -> None:
""" foo """
Expand All @@ -75,8 +56,9 @@ async def async_setup_platform(
) -> None:
"""Set up the platform."""
coordinator = await getCoordinator (hass, config)
async_add_entities([CTS600Sensor (coordinator, e, None) for e in _ENTITIES],
update_before_add=True)
await coordinator.updateData()
discovered_sensors = discover_sensors(coordinator.cts600)
async_add_entities([CTS600Sensor (coordinator, e, None) for e in discovered_sensors], update_before_add=True)

class CTS600Sensor(CoordinatorEntity, SensorEntity):
"""An entity using CoordinatorEntity.
Expand All @@ -94,23 +76,22 @@ def __init__(
) -> None:
"""Pass coordinator to CoordinatorEntity."""
super().__init__(coordinator)
self.var_name = description.key
# self._attr_name = DOMAIN + "_" + spec["name"]
# self._attr_state_class = spec["state-class"]
# self._attr_device_class = spec["device-class"]
# self._attr_native_unit_of_measurement = spec["unit"]

self._name = coordinator.name + " " + self.var_name
self._name = coordinator.name + " " + description.name
self._attr_device_info = coordinator.device_info
self.entity_description = description
self._attr_unique_id = f"serial-{self.coordinator.cts600.port}-{self.var_name}"
self._attr_unique_id = f"serial-{self.coordinator.cts600.port}-{description.key}"

@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
# _LOGGER.debug("Entity update: %s", self.coordinator.data)
value = self.coordinator.cts600.data.get(self.var_name)
if value:
value = self.coordinator.cts600.data.get(self.entity_description.key)
if value != self._attr_native_value:
self._attr_native_value = value
self.async_write_ha_state()

Expand Down

0 comments on commit 89654e3

Please sign in to comment.