Skip to content

Fix websocket connection sensor#22923

Merged
balloob merged 2 commits into
home-assistant:devfrom
Swamp-Ig:28890-websocket_api_sensor
Apr 13, 2019
Merged

Fix websocket connection sensor#22923
balloob merged 2 commits into
home-assistant:devfrom
Swamp-Ig:28890-websocket_api_sensor

Conversation

@Swamp-Ig
Copy link
Copy Markdown
Contributor

@Swamp-Ig Swamp-Ig commented Apr 9, 2019

Description:

Bug shows that websocket sensor gets out of sync and shows negative numbers. I suspect this is because there's connections made before the websocket sensor is started up.

Fixed by:

  • Keeping a set of connections in hass.data
  • Reading the size of the set directly

The set approach has the advantage that it we can look at the list of connections somewhere else, for example to display the list of current connections somewhere in the API if we choose to at some stage.

Related issue (if applicable): fixes #22890

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.

If the code does not interact with devices:

  • Tests have been added to verify that the new code works.

@Swamp-Ig Swamp-Ig requested a review from a team as a code owner April 9, 2019 07:35
@ghost ghost assigned Swamp-Ig Apr 9, 2019
@ghost ghost added the in progress label Apr 9, 2019
@Swamp-Ig Swamp-Ig changed the title Fix for #28890 Fix websocket connection sensor Apr 9, 2019
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 9, 2019

Hey there @home-assistant/core, mind taking a look at this pull request as its been labeled with a integration (websocket_api) you are listed as a codeowner for? Thanks!

@Swamp-Ig Swamp-Ig force-pushed the 28890-websocket_api_sensor branch from ca9293d to 1964c61 Compare April 9, 2019 08:02
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 9, 2019

Codecov Report

Merging #22923 into dev will decrease coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##              dev   #22923      +/-   ##
==========================================
- Coverage   93.83%   93.83%   -0.01%     
==========================================
  Files         448      448              
  Lines       36583    36585       +2     
==========================================
+ Hits        34329    34330       +1     
- Misses       2254     2255       +1
Impacted Files Coverage Δ
homeassistant/components/websocket_api/const.py 100% <100%> (ø) ⬆️
homeassistant/components/websocket_api/sensor.py 100% <100%> (ø) ⬆️
homeassistant/components/websocket_api/http.py 89.51% <100%> (+0.17%) ⬆️
homeassistant/components/uk_transport/sensor.py 93.43% <0%> (-0.73%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6ee23bd...3ed55c3. Read the comment docs.


self._logger.debug("Received %s", msg)
connection = await auth.async_handle(msg)
self._connections.add(connection)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

let's not add the connection, but instead store id(self), which we also use for logging.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That will work for this PR, although I was thinking of having a list of connected users in the dev-info page or somewhere, in which case keeping the connection set is needed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think that it would be overkill really. Just because we can get all the data, doesn't mean we should 😆

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In which case it's just as well to keep a bare count, rather than a set of anything.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

How this even work? WebSocketHandler is not a singleton.

Copy link
Copy Markdown
Contributor Author

@Swamp-Ig Swamp-Ig Apr 11, 2019

Choose a reason for hiding this comment

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

Ahh then it won't work! Maybe we should have just deleted the thing in the first place? Will fix properly.

@Swamp-Ig
Copy link
Copy Markdown
Contributor Author

That should fix it now.

@balloob balloob merged commit 2527731 into home-assistant:dev Apr 13, 2019
@ghost ghost removed the in progress label Apr 13, 2019
@Swamp-Ig Swamp-Ig deleted the 28890-websocket_api_sensor branch April 18, 2019 10:23
@Molodax
Copy link
Copy Markdown

Molodax commented Apr 29, 2019

The fix didn't work.

@cgtobi cgtobi added this to the 0.92.2 milestone Apr 29, 2019
@balloob balloob removed this from the 0.92.2 milestone May 2, 2019
@balloob
Copy link
Copy Markdown
Member

balloob commented May 2, 2019

Removed from milestone because it is already released

@Molodax
Copy link
Copy Markdown

Molodax commented May 2, 2019

So that means that the fix doesn't work if it has already been released prior to 0.92.2

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.

Websocket API sensor shows negative number of clients (e.g. -1)

6 participants