Skip to content

Conversation

@FredrikM97
Copy link
Contributor

@FredrikM97 FredrikM97 commented May 16, 2025

Proposed change

I have a sensor that provides a list of objects representing data from my pet feeder. However, the current implementation only supports ElectricityTypeData as the allowed TypeData for raw input. If no subkey is specified, the data is skipped and the sensor state becomes unknown. Additionally, ElectricityTypeData is not capable of decoding the pet feeder's data format. Therefore, a more flexible and extensible implementation is necessary to support custom data types like the pet feeder.

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

  • This PR fixes or closes issue: fixes Tuya sensor not display RAW values #144745
  • This PR is related to issue:
  • Link to documentation pull request:
  • Link to developer documentation pull request:
  • Link to frontend pull request:

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:

@home-assistant
Copy link

Hey there @tuya, @zlinoliver, mind taking a look at this pull request as it has been labeled with an integration (tuya) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of tuya can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign tuya Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

Copy link
Contributor

@davidrapan davidrapan left a comment

Choose a reason for hiding this comment

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

Hi @FredrikM97, and fix change type selection 😋

@FredrikM97
Copy link
Contributor Author

FredrikM97 commented May 17, 2025

Hi @FredrikM97, and fix change type selection 😋

@davidrapan What do you mean?

@FredrikM97
Copy link
Contributor Author

Updated to be better backwards compatible (default to ElectricityTypeData) and also fixed the type for the native_value.

@epenet
Copy link
Contributor

epenet commented Jul 9, 2025

I would suggest as a preliminary PR to add a fixture files with your pet feeder details.
Then this PR will be showing how "new" information gets automatically added to the tests.

  1. download the json diagnostics
  2. extract the data content from the diagnostics
  3. remove the homeassistant sub-section from the data
  4. save as tests/components/tuya/fixtures/{category}_{description}.json
  5. add the file to DEVICE_MOCKS in tests/components/tuya/__init__.py
  6. run pytest tests/components/tuya --snapshot-update to update the snapshot files

(see #148401 as a sample)

@FredrikM97
Copy link
Contributor Author

I would suggest as a preliminary PR to add a fixture files with your pet feeder details. Then this PR will be showing how "new" information gets automatically added to the tests.

  1. download the json diagnostics
  2. extract the data content from the diagnostics
  3. remove the homeassistant sub-section from the data
  4. save as tests/components/tuya/fixtures/{category}_{description}.json
  5. add the file to DEVICE_MOCKS in tests/components/tuya/__init__.py
  6. run pytest tests/components/tuya --snapshot-update to update the snapshot files

(see #148401 as a sample)

@epenet I could split the sensor code (meal_plan) to another PR instead. The main focus in this PR is to solve the RAW base64 and not force it to be a ElectricityTypeData.

@epenet
Copy link
Contributor

epenet commented Jul 9, 2025

@epenet I could split the sensor code (meal_plan) to another PR instead. The main focus in this PR is to solve the RAW base64 and not force it to be a ElectricityTypeData.

For sure everything that can be done in a separate PR is best

@FredrikM97
Copy link
Contributor Author

@epenet I've now separated it so that this PR focuses solely on allowing RAW data to be types other than ElectricityTypeData.

Copy link
Contributor

@epenet epenet left a comment

Choose a reason for hiding this comment

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

Conflicts need resolving

@home-assistant home-assistant bot marked this pull request as draft July 16, 2025 08:40
@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.

@FredrikM97 FredrikM97 force-pushed the tuya_support_different_raw_sensor_data_types branch from 6626098 to 073a73e Compare July 17, 2025 16:02
@FredrikM97 FredrikM97 marked this pull request as ready for review July 19, 2025 19:26
@home-assistant home-assistant bot requested a review from epenet July 19, 2025 19:26
@classmethod
def from_raw(cls, data: str) -> Self:
"""Return a RawTypeData object."""
return cls(raw=data)
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it not make sense to decode it with base64.b64decode?

Copy link
Contributor Author

@FredrikM97 FredrikM97 Jul 20, 2025

Choose a reason for hiding this comment

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

Preffered but high risk to hit the 256 limit. Maybe later if subtype is needed it make sense to decode it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Then I fail to see the value of the current base64 string.

Please adjust the PR description to highlight how it may be useful.
A sensor "just because it's available" is not generally approved unless there is a valid scenario.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here is an example of usage https://github.com/FredrikM97/mealplan-card

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Overall it feels wrong to force raw or json types to by ElectricityTypeData

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If that is the case then it sounds reasonable to use a service if possible. Still think we should change the ElectricityTypeData somehow to be more generic.

Copy link
Contributor

@epenet epenet Jul 21, 2025

Choose a reason for hiding this comment

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

I agree - but I'm not sure if that will be approved if there is no other BaseType (that is why I suggested exposing other sensors)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What about keeping base class and separate the Mealplan to a service? Then it will be easier for future to add new typeData for raw/json.

If you have an idea how to expose a list of sensors for up to 10 schedules without overcomplicate it I'm all ears.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think you need to add all schedules...
But maybe showing four sensors has value:

  • Total schedules: 5
  • Active schedules: 2
  • Today feed amount: 15 g
  • Weekly feel amount: 150 g

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How do we decide what fields exist? If you check the Mealplan card there is profiles depending on device. Since base64 does not map the same for all devices. Can't come up with a good workaround for that.

@home-assistant home-assistant bot marked this pull request as draft July 20, 2025 07:51
@FredrikM97 FredrikM97 closed this Sep 2, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Sep 3, 2025
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.

Tuya sensor not display RAW values

3 participants