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

When state is unavailable the NetDaemon treat is as Null state. #84

Merged
merged 1 commit into from
Apr 18, 2020

Conversation

helto4real
Copy link
Collaborator

@helto4real helto4real commented Apr 18, 2020

Breaking change

It can be breaking if you rely on:

if (entity.State == "unavailable") ...

after:

if (entity.State is null) ...

Proposed change

The changes the behavior for entity states that are unavailable. Any state that is unavailable is set to null value. You now do not have to check for unavailable in the scenarios you expect state to be a number as an example.

this code breaks now but will be fine with this PR since state is null when unavailable value to the state.

        public async override Task InitializeAsync() => Entity("sensor.hacs")
                .WhenStateChange((n, o) =>
                    o?.State != n?.State && n?.State > 0)
                        .Call(async (entityId, newState, oldState) =>
                        {
                            var serviceDataTitle = "Updates pending in HACS";
                            var serviceDataMessage = "There are updates pending in [HACS](/hacs)\n\n";


                            List<object> repositories = newState?.Attribute?.repositories || new List<object>();
                            foreach (IDictionary<string, object?> item in repositories)
                            {
                                serviceDataMessage += $"- {item?["display_name"].ToString()}\n";
                            }

                            await CallService("persistent_notification", "create", new
                            {
                                title = serviceDataTitle,
                                message = serviceDataMessage
                            }, true);
                        }).Execute();

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (which adds functionality to an existing integration)
  • 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][dev-checklist]
  • The code compiles without warnings (code quality chek)
  • Tests have been added to verify that the new code works.

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

@helto4real helto4real merged commit 47892f3 into dev Apr 18, 2020
@helto4real helto4real deleted the unavailable_state_is_null branch April 18, 2020 18:04
Ikcelaks pushed a commit to Ikcelaks/netdaemon that referenced this pull request Dec 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature: Make State return null if State is 'unavailable'
1 participant