Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CCL Electronics integration #130281

Open
wants to merge 24 commits into
base: dev
Choose a base branch
from
Open

Add CCL Electronics integration #130281

wants to merge 24 commits into from

Conversation

fkiscd
Copy link

@fkiscd fkiscd commented Nov 10, 2024

Proposed change

Adds the CCL Electronics integration, allowing local weather stations with expandable modules to push sensor data.

Link to library: https://github.com/CCL-Electronics-Ltd/aioccl

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.

To help with the load of incoming pull requests:

Copy link

@home-assistant home-assistant bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @fkiscd

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

Copy link

@home-assistant home-assistant bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mikey0000
Copy link
Contributor

mikey0000 commented Nov 10, 2024

Need to only PR one platform at a time.

Copy link

@home-assistant home-assistant bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @fkiscd

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

Copy link

@home-assistant home-assistant bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @fkiscd

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

Copy link

@home-assistant home-assistant bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @fkiscd

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

entry.runtime_data = CCLDevice(entry.data["passkey"])

CCLServer.add_copy(entry.runtime_data)
await CCLServer.run()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are launching your own webserver here which is not something we want. There is a built in webhook component that can be used to allow push style HTTP callbacks, take a look at tedee for how those are registered. Also, is there any way you can push the instance details to CCL, instead of the user having to go to the app to configure that manually?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the advice. It is now changed to listen for requests using webhook api.
Here is the test command I input to the zsh terminal in dev container (about half of them are binary sensors which are not included in this PR):
curl localhost:8123/api/webhook/c2507495 -X POST -H 'Content-Type: application/json' -d '{"serial_no": "1061616322","mac_address": "48:31:B7:06:D5:59","model":"HA100","fw_ver":"v1.00","datetime":"2024-12-26+02:47:46","abar":1014.1,"rbar":1012.9,"t1dew":16.1,"t1feels":22.9,"t1chill":22.9,"inhum":63,"intem":23.0,"t1solrad":0,"t1hum":66,"t1tem":22.9,"t1rainra":0,"t1raindy":0,"t1rainhr":0,"t1rainmth":0,"t1rainwy":0,"t1rainyr":0,"t1uvi":0,"t1wdir":276,"t1wgust":0,"t1ws":0,"t1ws10mav":0,"inbat":1,"t1bat":0,"t234c1cn":0,"t234c2cn":0,"t234c3cn":0,"t234c4cn":0,"t234c5cn":0,"t234c6cn":0,"t234c7cn":0,"t11cn":0,"t10cn":0,"t9cn":0,"t6c1cn":0,"t6c2cn":0,"t6c3cn":0,"t6c4cn":0,"t6c5cn":0,"t6c6cn":0,"t6c7cn":0,"t5lscn":0,"t1cn":1,"t8cn":0,"api_ver":"v1.00"}'

@home-assistant home-assistant bot marked this pull request as draft December 17, 2024 14:31
@home-assistant
Copy link

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@fkiscd fkiscd marked this pull request as ready for review December 28, 2024 16:31
@home-assistant home-assistant bot dismissed stale reviews from themself December 28, 2024 16:31

Stale

PLATFORMS: list[Platform] = [Platform.SENSOR]


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please register a custom type for the entry


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up a config entry for a single CCL device."""
entry.runtime_data = CCLDevice(entry.data[CONF_WEBHOOK_ID])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

preference: put stuff only in the runtime data once the rest is done

async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up a config entry for a single CCL device."""
entry.runtime_data = CCLDevice(entry.data[CONF_WEBHOOK_ID])
CCLServer.register(entry.runtime_data)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this fail?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it will never fail if CONF_WEBHOOK_ID exists

VERSION = 1
_webhook_id: str

async def async_step_user(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how does this actually work? Like how do you know which weather station to register with, without the user being able to input anything?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It generates a random path. We have a mobile app detecting nearby devices, data will be pushed once you input that path in the configuration of a certain device.

Comment on lines 39 to 56
@property
def device_name(self) -> str:
"""Return the device name."""
if self.internal.compartment is not None:
return self.device.name + " " + self.internal.compartment
return self.device.name

@property
def device_id(self) -> str:
"""Return the 6-digits device id."""
if self.internal.compartment is not None:
return (
(self.device_name + "_" + self.internal.compartment)
.replace(" ", "")
.replace("-", "_")
.lower()
)
return self.device_name.replace(" ", "").replace("-", "_").lower()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like those properties if they are only used in init (tbh not sure that even works?)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed them as properties

async_add_entities: AddEntitiesCallback,
) -> None:
"""Add sensors for passed config entry in HA."""
device: CCLDevice = entry.runtime_data
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
device: CCLDevice = entry.runtime_data
device = entry.runtime_data

will be inferred once you have the custom entry type

Comment on lines +189 to +190
device.register_new_sensor_cb(_new_sensor)
entry.async_on_unload(lambda: device.remove_new_sensor_cb(_new_sensor))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's happening here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just appending the add sensor callback to a list in the device, to be executed when new sensor data is received. I'm also removing that from the list on entry unload.

@home-assistant home-assistant bot marked this pull request as draft January 7, 2025 13:03
@fkiscd fkiscd marked this pull request as ready for review January 31, 2025 07:57
@home-assistant home-assistant bot requested a review from zweckj January 31, 2025 07:57
Copy link
Member

@joostlek joostlek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New integrations are required to have a quality_scale.yaml and reach at least bronze. Please add that and make sure you implement the required rules

Comment on lines 28 to 29
if CONF_WEBHOOK_ID is None:
return False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would a constant be None?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I worried it might cause errors in the CCLServer.register, but it doesn't. These lines have been deleted.

Comment on lines 31 to 38
CCLServer.register(new_device)

async def register_webhook() -> None:
def handle_webhook(
hass: HomeAssistant, webhook_id: str, request: web.Request
) -> Any:
"""Handle incoming webhook from CCL devices."""
return CCLServer.handler(request)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the CCLServer?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't run a server by itself but rather provides functions to be imported. The CCLServer.register adds new_device to a dictionary of devices with the key CONF_WEBHOOK_ID. Whenever a webhook catches an HTML request pushed from a device, the CCLServer.handler will process data and update the internal values of the device sensors through the dictionary.

@home-assistant home-assistant bot marked this pull request as draft January 31, 2025 09:43
@fkiscd fkiscd marked this pull request as ready for review February 4, 2025 11:36
@home-assistant home-assistant bot requested a review from joostlek February 4, 2025 11:36
@CCLMoSt
Copy link

CCLMoSt commented Feb 24, 2025

@joostlek @zweckj @ Could you please provide the timeline for the completion of the review?

@zweckj
Copy link
Member

zweckj commented Feb 24, 2025

@joostlek @zweckj @ Could you please provide the timeline for the completion of the review?

No, sorry. Not because we don't want to, but because we can't. This is an open source project after all, so this will depend on reviewer availability and your response times.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants