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 LedSC integration #116957

Closed
wants to merge 29 commits into from
Closed

Add LedSC integration #116957

wants to merge 29 commits into from

Conversation

PatrikKr010
Copy link

@PatrikKr010 PatrikKr010 commented May 6, 2024

Breaking change

Proposed change

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.
  • Untested files have been added to .coveragerc.

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 @PatrikKr010

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!

@PatrikKr010 PatrikKr010 marked this pull request as ready for review May 8, 2024 15:56
@PatrikKr010
Copy link
Author

Documentation PR

Copy link
Contributor

@tr4nt0r tr4nt0r left a comment

Choose a reason for hiding this comment

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

An Integration may not communicate directly with devices or services as stated in the development checklist:
https://developers.home-assistant.io/docs/development_checklist/

@PatrikKr010
Copy link
Author

PatrikKr010 commented May 27, 2024

Hi @tr4nt0r,
Communication is done using websocket. The pythoni library requests, which is available in PyPi, is used for this. This library has been added to requirements.

I wrote the WebSC application with which the communication takes place in the interaction. This application is used to remove the hardware part and transfer it to the websocket API.

I can separate the integration part into a separate library that will take care of the communication with WebSC, but I find it redundant.

Is this separation necessary?

@tr4nt0r
Copy link
Contributor

tr4nt0r commented May 27, 2024

Hi @tr4nt0r, Communication is done using websocket. The pythoni library requests, which is available in PyPi, is used for this. This library has been added to requirements.

I wrote the WebSC application with which the communication takes place in the interaction. This application is used to remove the hardware part and transfer it to the websocket API.

I can separate the integration part into a separate library that will take care of the communication with WebSC, but I find it redundant.

Is this separation necessary?

requests is already part of Home Assistant, so it's quite redundant to add it to the manifest but anyway, the maintainers are pretty strict about this and requests is simply not considered a device specific abstraction layer, it is just an http client

@PatrikKr010
Copy link
Author

Hi @tr4nt0r
I added an abstract layer that handles communication with WebSC Websocket API. This layer is the websc-client library. I hope this abstraction is OK.

@PatrikKr010 PatrikKr010 requested a review from tr4nt0r May 29, 2024 23:21
Copy link
Contributor

@tr4nt0r tr4nt0r left a comment

Choose a reason for hiding this comment

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

It is required to have tests at least for the config flow. Files that are not covered by tests must be added to .coveragerc

homeassistant/components/ledsc/config_flow.py Outdated Show resolved Hide resolved
homeassistant/components/ledsc/config_flow.py Outdated Show resolved Hide resolved
homeassistant/components/ledsc/config_flow.py Outdated Show resolved Hide resolved
homeassistant/components/ledsc/config_flow.py Outdated Show resolved Hide resolved
homeassistant/components/ledsc/config_flow.py Show resolved Hide resolved
homeassistant/components/ledsc/light.py Outdated Show resolved Hide resolved
homeassistant/components/ledsc/light.py Outdated Show resolved Hide resolved
homeassistant/components/ledsc/config_flow.py Outdated Show resolved Hide resolved
return not self._websc.is_lost

@property
def name(self) -> str:
Copy link
Contributor

Choose a reason for hiding this comment

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

Use the entity_description attribute instead and use strings.json for device names instead

Copy link
Author

Choose a reason for hiding this comment

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

I can use entity_description for more detailed device information, but the entity name is dynamic based on the WebSC configuration.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think you don't even need a name for the entity, if you leave it empty, and set the name of the device and the manufacturer in the DeviceInfo it will use the generic name "Light", the entity will then be named light.ledsc_devicename_light

Copy link
Author

Choose a reason for hiding this comment

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

image

Do is not a very nice default name.
If you don't mind, I would like to leave this property. This name is set in WebSC.
Snímek obrazovky z 2024-05-30 18-33-19

Copy link
Contributor

Choose a reason for hiding this comment

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

It's tricky to get this right, haven't done this yet with the light platform. I would like to test it, is there some way to do this? The host demo.ledsc.eu did not work.

Copy link
Author

Choose a reason for hiding this comment

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

The demo.ledsc.eu domain is still under development. I am currently creating a way to create a virtual instance of WebSC so that anyone who wants to try the demo can run their own instance of WebSC for a limited time. However, for testing purposes I can run one static instance on this domain. Otherwise you can run your own instances of course.

Copy link
Author

Choose a reason for hiding this comment

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

homeassistant/components/ledsc/manifest.json Show resolved Hide resolved
Copy link
Contributor

@tr4nt0r tr4nt0r left a comment

Choose a reason for hiding this comment

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

Personally, I wouldn't add a comment here, but according to the checklist, the code must pass the check using the python3 -m script.hassfest command, which requires docstring on methods.
In this state the test passes without any errors.

The first line describing the function is ok, what i meant was the second line.

@PatrikKr010
Copy link
Author

It is required to have tests at least for the config flow. Files that are not covered by tests must be added to .coveragerc

Done

Copy link
Contributor

@tr4nt0r tr4nt0r left a comment

Choose a reason for hiding this comment

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

It is required to have tests at least for the config flow. Files that are not covered by tests must be added to .coveragerc

Done

You NEED to add testing for the config flow, it is mandatory for new integrations. If you run scaffold in VS Code and select that you want to add config flow to your integration, it will create some example test files, that should be a good starting point

Copy link
Contributor

@tr4nt0r tr4nt0r left a comment

Choose a reason for hiding this comment

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

I used this way of identification to make it unique for the same connection and it was not possible to connect more integrations to the same WebSC server.

Is this ID global or must it be unique only in my integrations? If is global, I suggest f"ledsc:{host}:{port}".

Please look at the requirements for unique ids https://developers.home-assistant.io/docs/entity_registry_index/#unique-id-requirements

homeassistant/components/ledsc/light.py Outdated Show resolved Hide resolved
homeassistant/components/ledsc/consts.py Outdated Show resolved Hide resolved
homeassistant/components/ledsc/config_flow.py Outdated Show resolved Hide resolved
homeassistant/components/ledsc/config_flow.py Outdated Show resolved Hide resolved
homeassistant/components/ledsc/config_flow.py Outdated Show resolved Hide resolved
homeassistant/components/ledsc/light.py Outdated Show resolved Hide resolved
Copy link
Contributor

@tr4nt0r tr4nt0r left a comment

Choose a reason for hiding this comment

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

I used this way of identification to make it unique for the same connection and it was not possible to connect more integrations to the same WebSC server.

Is this ID global or must it be unique only in my integrations? If is global, I suggest f"ledsc:{host}:{port}".

You have _async_abort_entries_match to make sure, there is only one config entry per ledsc server. That should do the job.

tests/components/ledsc/test_config_flow.py Outdated Show resolved Hide resolved
tests/components/ledsc/test_config_flow.py Outdated Show resolved Hide resolved
tests/components/ledsc/test_config_flow.py Outdated Show resolved Hide resolved
tests/components/ledsc/test_config_flow.py Outdated Show resolved Hide resolved
tests/components/ledsc/test_config_flow.py Outdated Show resolved Hide resolved
tests/components/ledsc/test_config_flow.py Outdated Show resolved Hide resolved
tests/components/ledsc/test_config_flow.py Outdated Show resolved Hide resolved
tests/components/ledsc/test_config_flow.py Outdated Show resolved Hide resolved
tests/components/ledsc/test_config_flow.py Outdated Show resolved Hide resolved
.coveragerc Outdated Show resolved Hide resolved
Copy link
Contributor

@tr4nt0r tr4nt0r left a comment

Choose a reason for hiding this comment

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

Your integration is still missing translations. Please add a strings.json file.

@PatrikKr010
Copy link
Author

I fixed the tests. Now all tests are passing. I added a strings.json file, but it doesn't work. But I don't know if I understand its principle correctly.

@PatrikKr010 PatrikKr010 requested a review from tr4nt0r June 2, 2024 12:46
Copy link
Contributor

@tr4nt0r tr4nt0r left a comment

Choose a reason for hiding this comment

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

Please address all changes i requested before we can continue with the review process.

tests/components/ledsc/__init__.py Outdated Show resolved Hide resolved
homeassistant/components/ledsc/__init__.py Outdated Show resolved Hide resolved
homeassistant/components/ledsc/__init__.py Outdated Show resolved Hide resolved
Comment on lines +22 to +27
load the configured devices from WebSC Server and add them to hass.
"""
client = WebSClient(host=config.data[CONF_HOST], port=config.data[CONF_PORT])
await client.connect()
hass.async_create_background_task(client.observer(), name="ledsc-observer")

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
load the configured devices from WebSC Server and add them to hass.
"""
client = WebSClient(host=config.data[CONF_HOST], port=config.data[CONF_PORT])
await client.connect()
hass.async_create_background_task(client.observer(), name="ledsc-observer")
load the configured devices from WebSC Server and add them to hass.
"""
client = config.runtime_data

Initialization of the client should be done in init.py and then stored in the config entry. See https://developers.home-assistant.io/blog/2024/04/30/store-runtime-data-inside-config-entry on how to do this.

Comment on lines +42 to +54

def __init__(
self,
client_id: str,
websc: WebSCAsync,
hass: HomeAssistant,
) -> None:
"""Initialize an LedSC Light."""
self._hass: HomeAssistant = hass
self._websc: WebSCAsync = websc
self._attr_unique_id = f"{client_id}-{websc.name}"
_LOGGER.debug(f"LedSC '%s' initialized!", self.name)

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
def __init__(
self,
client_id: str,
websc: WebSCAsync,
hass: HomeAssistant,
) -> None:
"""Initialize an LedSC Light."""
self._hass: HomeAssistant = hass
self._websc: WebSCAsync = websc
self._attr_unique_id = f"{client_id}-{websc.name}"
_LOGGER.debug(f"LedSC '%s' initialized!", self.name)
_attr_has_entity_name = True
_attr_translation_key: str = "light"
def __init__(
self,
client_id: str,
websc: WebSCAsync,
hass: HomeAssistant,
) -> None:
"""Initialize an LedSC Light."""
self._hass: HomeAssistant = hass
self._websc: WebSCAsync = websc
self._attr_unique_id = f"{client_id}-{websc.name}"
_LOGGER.debug("LedSC %s initialized!", websc.name)
self._attr_device_info = DeviceInfo(
manufacturer="LedSC",
model="LedSC",
name=websc.name,
identifiers={(DOMAIN, f"{client_id}-{websc.name}")},
)

Ok, so I fiddled around a bit and this is what i came up with. No need to set entity_description. It will create a Device with the corresponding Name from the ledsc server. The entity will have a generic name light.

You also will have to add the following to the strings.json file

"entity": {
    "light": {
      "light": {
        "name": "[%key:component::light::title%]"
      }
    }
  }

and as LedSC are led-strips you may also want to add an icons.json and set the icon to a led-strip

{
  "entity": {
    "light": {
      "light": {
        "default": "mdi:led-strip-variant"
      }
    }
  }
}

homeassistant/components/ledsc/config_flow.py Outdated Show resolved Hide resolved
homeassistant/components/ledsc/light.py Outdated Show resolved Hide resolved
homeassistant/components/ledsc/strings.json Outdated Show resolved Hide resolved
Comment on lines +64 to +68
await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_USER},
data=USER_INPUT,
)
Copy link
Contributor

Choose a reason for hiding this comment

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

You are testing if the config flow aborts, if an entry already exists. To be able to test this, you set the precondition, that there is already an entry, you just add it to hass, then initialize the config flow.

@tr4nt0r
Copy link
Contributor

tr4nt0r commented Jun 19, 2024

Also i had the impression you are the manufacturer of these devices or somehow associated with them, or am i wrong? If so, maybe this could be interesting for you:

@joostlek
Copy link
Member

Hey @PatrikKr010,

I just checked your website, ledsc.eu, and apparently I can't order anything. Could you please elaborate on what your plans are and what the product is?

Thanks!

@PatrikKr010
Copy link
Author

Hello,
Yes, the product cannot be ordered yet. The development and testing is completely finished. We are currently working on the launch of production and arranging the distribution of our product.
As soon as we have finished this work and the integration into Home assistant is released, we will start selling our product.

@joostlek
Copy link
Member

Thanks for contribution and for the information about the current state of the product. We appreciate the effort you’ve put into developing this integration.

However, we only include integrations for products or services that are currently available for our users. This ensures that our community can immediately benefit from and support the integration in further open source development.

We recommend publishing your integration as a custom component for now. Once your product is available on the market and the custom integration has had time to mature, we would be happy to re-evaluate and review a new pull request for inclusion of your integration in the core repository.

Thank you for understanding, and looking forward to seeing your product’s hitting the market!

@joostlek joostlek closed this Jun 27, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jun 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants